How to plot separately figures?

2 Ansichten (letzte 30 Tage)
Talha Gungor
Talha Gungor am 21 Mai 2021
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 Kommentar
Jaya
Jaya am 22 Mai 2021
What is the problem you are facing? Please state it. Because the code is producing the graphs.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Sulaymon Eshkabilov
Sulaymon Eshkabilov am 22 Mai 2021
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

Weitere Antworten (0)

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by