How to plot separately figures?

조회 수: 4 (최근 30일)
Talha Gungor
Talha Gungor 2021년 5월 21일
답변: Sulaymon Eshkabilov 2021년 5월 22일
I want to plot a second figure with 2x1 subplot, the first subplot must contain both x (original signal) and xn(noise added signal), and second subplot must contain x (original signal) and y(filtered signal). What should I change ?
fs = 5000;
tiv=1/fs;
t = 0:tiv:0.05;
t1=2*pi*100*t;
t2=2*pi*500*t;
x = sin(t1)+0.7*cos(t2);
xn = x + 5.*randn(size(t));
originalSpectrum = fft(x);
noisySpectrum = fft(xn);
xdb=mag2db(originalSpectrum);
xndb=mag2db(abs(noisySpectrum));
figure(1)
subplot(2, 1, 1);
plot((xdb),'b-', 'LineWidth', 2);
xlabel('Frequency (Hz)')
ylabel('Magnitude(dB)')
title('Original Signal (No Noise)')
subplot(2, 1, 2);
plot(abs(xndb), 'b-', 'LineWidth', 2);
xlabel('Frequency (Hz)')
ylabel('Magnitude(dB)')
title('Noisy Signal')
d = designfilt('lowpassfir', 'PassbandFrequency', 500, ...
'StopbandFrequency', 600, ...
'PassbandRipple', 1, 'StopbandAttenuation', 100, ...
'SampleRate', fs);
y=filter(d,x);
figure(2)
subplot(2,1,1);
plot(x); hold on; plot(xn);
subplot(2,1,2);
plot(x); hold on; plot(y);
  댓글 수: 1
Jaya
Jaya 2021년 5월 22일
What is the problem you are facing? Please state it. Because the code is producing the graphs.

댓글을 달려면 로그인하십시오.

채택된 답변

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021년 5월 22일
Here are the plot parts as you were aiming to get:
...
subplot(2,1,1);
plot(t, x, 'b', t, xn, 'r'); grid on
legend('Original Signal', 'Noise Added Signal')
ylabel('Signal')
subplot(2,1,2);
plot(t, x, 'b', t, y, 'r');
legend('Original Signal', 'Filtered Signal', 'location', 'southwest')
ylabel('Signal'), xlabel('Time')
Good luck

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Fourier Analysis and Filtering에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by