how to multiply two signals given step signal of duration 0.2 and cosine wave ,and i need to plot the spectrogram of the output ,some how managed to get product but spectrogram is happening

9 次查看(过去 30 天)
fs=1000;
t=0:1/fs:2.0-1/fs;
Z=length(t);
f1=20;
f2=30;
f3=40;
x=3*cos(2*pi*f1*(t)+0.2)+cos(2*pi*f2*(t)-0.3)+2*cos(2*pi*f3*(t)+2.4);
subplot(3,3,1);
plot(t,x);
title(['inputwave']);
xlabel('time');
ylabel('amplitude');
f=fft(x);
m=length(f);
xmag=abs(f);
subplot(3,3,2);
plot(xmag);
title(['fft of input']);
xlabel('hz');
ylabel('magnitude');
fs1=1000; %sampling frequency
T=0.2; %width of the rectangule pulse in seconds
m=-1:1/fs1:1-1/(fs1); %time base
z=length(m);
r=rectpuls(m-(T)/2,T); %generating the square wave
subplot(3,3,3);
plot(m,r,'k');
title(['Rectangular Pulse width=', num2str(T),'s']);
xlabel('Time(s)');
ylabel('Amplitude');
s=transpose(r)*(m);%multiplying both waves
subplot(3,3,4);
plot(t,s);
title(['multiplied output']);
xlabel('time(s)');
ylabel('amp');
k=fft(s);%fft of the windowed signal
kmag=abs(k);
subplot(5,1,5);
plot(kmag);
title(['fft of output']);
xlabel('frquency');
ylabel('amplitude');
spectrum of input and output
subplot(3,3,5);
title('spectro gram of input ');
spectrogram(x)
subplot(3,3,6);
title('spectro gram of output or product siganl');
spectrogram(s)

回答(1 个)

Dev
Dev 2025-8-26,9:33
In the code provided in the question, you're trying to multiply a step signal with a cosine wave and analyse the results. This code has some issues, particularly with the multiplication step and spectrogram plotting. Please find below the required improvements in the code:
  • Fixed multiplication: Use element-wise multiplication (.*) instead of matrix multiplication.
  • Proper time alignment: Both signals should use the same time vector t”.
  • Improved FFT plotting: We can use the “fftshift” function available in MATLAB to centre the spectrum around 0 Hz.
For more information on the usage of this function, please refer to the link below-
After making the above modifications, we can expect the input signal to be three constant frequency lines at 20Hz, 30Hz, and 40Hz throughout the duration. Also, the output signal will have the same three frequencies, but only visible during the 0.2s window when the rectangular pulse is active (1), and silent elsewhere (0). Please refer to the attached spectrogram plotted below-
Also, I have attached the graph for the input and output signals below, FYR.
I hope the above changes helps to resolve your query.

类别

Help CenterFile Exchange 中查找有关 Time-Frequency Analysis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by