How to write code for a composite signal with instantaneous frequency
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
Anyone can help me with writing code for this function?

Accepted Answer
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;
8 Comments
Hi Swetha,
Thanks a lot for the answer. When I use this script I get "Incorrect dimensions for matrix multiplication" error. Previously I wrote the time interval and composite signal code in another method and had the same error message.
Meanwhile, when plotting the signal might looks like this:

I really appreciate if you can tackle this challenge.
Best regards,
Ali
Hi Jan Ali
Sc= Signal1*rect1+Signal2*rect2+Signal3*rect3;
Replace the above line of code with following.
Sc= Signal1.*rect1+Signal2.*rect2+Signal3.*rect3;
Note that the signals(signal1, signal2, signal3) used in the code are different from the signals that you needed. To get the graph you needed , you have to change those signals.
Jan Ali
on 5 Dec 2020
Thanks Swetha, however the plot doesn't look like the one which is required. Also when I write the second signal as it is seen on the picture, I get "Matrix dimensions must agree"error.
Hi Jan Ali,
You might have used matrix multiplication instead of elementwise multiplication. Following code snippet is for better understanding
C= A*B % This is matrix multiplication
D=E.*F % This is element wise multiplication
% Try replacing "*" with ".*"
Jan Ali
on 8 Dec 2020
Thanks Swetha,
I tried a lot, with different methods either * or .*, but still the graph does not look like the expected one. Using the .* for the 2nd signal, I got the "Matrix dimensions must agree" message.
Warm regards,
Ali
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.
Jan Ali
on 9 Dec 2020
Thanks so much dear Swetha. As you notice, there is a transient in the 0,1 to 0,18 s time interval. I also got the same plat as you did, however not able to get the same as the picture above. Anyway, I really appreciate your effort and the time you spent to get the same result.
Wish you all the bests!
Warm regards,
Ali
Jan Ali
on 10 Jan 2021
Dear Swetha,
I found a workaround for this problem :)
fs=2000;
t = linspace(0,0.3,1000);
s1 = sin(2*pi*30*t) + 0.2*sin(2*pi*70*t);
s2 = sin(2*pi*30*t) + 0.2*sin(2*pi*70*t) + sin(2*pi*500*t).*exp(-30*t-0.1);
s3 = sin(2*pi*30*t) + 0.2*sin(2*pi*70*t);
if 0.1 > t >= 0
s1;
elseif 0.18 > t >= 0.1
s2;
else
s3;
end
s=[s1 s2 s3];
t = linspace(0,0.3,3000);
plot(t, s, 'b')
I really appreciate your kind support and the time you spent for coding.
Wish you all the best
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!