2 views (last 30 days)

Anyone can help me with writing code for this function?

Swetha Polemoni
on 23 Nov 2020

Hi Jan Ali,

It is my understanding that you want to add three signals that are defined only in particular intervels of time. Following code might help you.

t = (-1:0.01:1); % defining time axis

unitstep1=t>=0; % unitsetp1 takes 1 only when t is greater than equal to 0

unitstep2=t>=0.1; % unitsetp2 takes 1 only when t is greater than equal to 0.1

unitstep3=t>=0.18;% unitsetp3 takes 1 only when t is greater than equal to 0.18

unitstep4=t>=0.3; % unitsetp4 takes 1 only when t is greater than equal to 0.3

rect1=unitstep1-unitstep2; % rect1 takes 1 only when 0<=t<0.1

rect2=unitstep2-unitstep3; % rect2 takes 1 only when 0.1<=t<0.18

rect3=unitstep3-unitstep4; % rect2 takes 1 only when 0.18<=t<0.3

% taking three random sine siganls

Signal1=sin(2*pi*50*t);

Signal2=sin(2*pi*150*t);

Signal3=sin(2*pi*500*t);

% Composite signal

Sc= Signal1*rect1+Signal2*rect2+Signal3*rect3;

Swetha Polemoni
on 9 Dec 2020

Hi Jan Ali,

t = 0:0.0000001:0.3; % defining time axis

unitstep1=t>=0; % unitsetp1 takes 1 only when t is greater than equal to 0

unitstep2=t>=0.1; % unitsetp2 takes 1 only when t is greater than equal to 0.1

unitstep3=t>=0.18;% unitsetp3 takes 1 only when t is greater than equal to 0.18

unitstep4=t>=0.3; % unitsetp4 takes 1 only when t is greater than equal to 0.3

rect1=unitstep1-unitstep2; % rect1 takes 1 only when 0<=t<0.1

rect2=unitstep2-unitstep3; % rect2 takes 1 only when 0.1<=t<0.18

rect3=unitstep3-unitstep4; % rect2 takes 1 only when 0.18<=t<0.3

% taking three random sine siganls

Signal1=sin(2*pi*50*t)+0.2*sin(2*pi*250*t);

Signal2=sin(2*pi*50*t)+0.2*sin(2*pi*250*t)+sin(2*pi*500*t).*exp(-30*t-0.1);

Signal3=sin(2*pi*50*t)+0.2*sin(2*pi*250*t);

% Composite signal

Sc= Signal1.*rect1+Signal2.*rect2+Signal3.*rect3;

plot(t,Sc);

This is the plot I got when I tried to implement the composite signal you have provided. I can notice there is bit difference. I have plotted to check if rect1, rect2 and rect3 are in the given range and those are in correct range. Check if there is any change in signals to get the plot you are expecting.

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
## 0 Comments

Sign in to comment.