where could my error

numSamples = 250;
t = linspace(0,50,numSamples);
x = sin(2*pi*100*t) + 0.7*cos(2*pi*500*t);
xn = x + 5.*randn(size(t));

回答(1 个)

I bellieve ‘NumSamples’ should be larger.
Try this —
Fs = 5000; % <- ADDED
Fn = Fs/2; % <- ADDED
numSamples = 250*Fs; % <- CHANGED
t = linspace(0,50,numSamples);
x = sin(2*pi*100*t) + 0.7*cos(2*pi*500*t);
xn = x + 5.*randn(size(t));
originalSpectrum = fft(x);
noisySpectrum = fft(xn);
Fv = linspace(0, 1, fix(numel(t)/2)+1)*Fn; % One-Sided Fourier Transform Frequency Vector
Iv = 1:numel(Fv); % Corresponding Index Vector
figure
subplot(4, 1, 1);
plot(Fv, abs(originalSpectrum(Iv))*2, 'b-', 'LineWidth', 2);
grid on;
xlabel('t');
ylabel('x');
subplot(4, 1, 2);
plot(Fv, abs(noisySpectrum(Iv))*2, 'b-', 'LineWidth', 2);
dee = designfilt('lowpassfir', 'PassbandFrequency', 500, ...
'StopbandFrequency', 600, ...
'PassbandRipple', 1, 'StopbandAttenuation', 100, ...
'SampleRate', 5000);
dataOut = filter(dee,xn);
subplot(4, 1, 3);
plot(dataOut);
trfcn = fft(dataOut)./noisySpectrum;
subplot(4,1,4) % <- ADDED
plot(Fv, abs(trfcn(Iv)))
title('Transfer Function Of Filter')
I did not change anything of significance, just added some tweaks to make it a bit easier to understand.

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

提问:

2021-5-20

编辑:

2021-5-21

Community Treasure Hunt

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

Start Hunting!

Translated by