What is wrong with my filter?
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Mantas Kirna
am 3 Jun. 2020
Kommentiert: Star Strider
am 3 Jun. 2020
Why is it filtering the wrong frequencies in the middle bands?
%Sample frequency
fs = 64000; %Hz
%Sample time
T=1/fs;
%Number of samples
Length = 300;
%Time samples
t=(0:Length-1)*T;
%Signal formation
Signal=2*sin(2*pi*1000*t)+0.5*sin(2*pi*3000*t)+2*sin(2*pi*5000*t)+0.5*sin(2*pi*7000*t)+2*sin(2*pi*9000*t)+0.5*sin(2*pi*11000*t)+2*sin(2*pi*13000*t);
%Plot signal
figure(1);
plot(t, Signal);
xlabel('Time (Length / Fs) , s');
ylabel('Amplitude');
title('Sum of 1 kHz and 15 kHz')
%Spectrum
Y=fft(Signal);
%Separate sides
P2 = abs(Y/Length);
P1 = P2(1:Length/4+1);
P1(2:end-1) = 2*P1(2:end-1);
%Plot spectrum
f = fs*(0:(Length/4))/Length;
figure(2);
plot(f,P1)
title('Single-Sided Amplitude Spectrum')
xlabel('f, Hz')
ylabel('P')
% All frequency values are in kHz.
Fs = 28; % Sampling Frequency
N = 48; % Order
F = [0 2 2.1 3.9 4 6 6.1 7.9 8 10 10.1 11.9 12 14]; % Frequency Vector
A = [4 4 0 0 4 4 0 0 4 4 0 0 4 4]; % Amplitude Vector
W = [1 100 1 100 1 100 1]; % Weight Vector
% Calculate the coefficients using the FIRPM function.
b = firpm(N, F/(Fs/2), A, W);
Hd = dfilt.dffir(b);
%Filter the signal
Signal_filtered = filter(b,1,Signal);
%Plot filtered signal
figure(3);
plot(t, Signal_filtered);
xlabel('Time (Length / Fs) , s');
ylabel('Amplitude');
title('Filtered signal (Cutoff Frequency 5 kHz)')
%Spectrum of filtered signal
Y=fft(Signal_filtered);
%Separate sides
P2 = abs(Y/Length);
P1 = P2(1:Length/4+1);
P1(2:end-1) = 2*P1(2:end-1);
%Plot spectrum of filtered signal
f = fs*(0:(Length/4))/Length;
figure(4);
plot(f,P1)
title('Single-Sided Amplitude Spectrum of the filtered signal (Cutoff Frequency 5 kHz)')
xlabel('f, Hz')
ylabel('P')
0 Kommentare
Akzeptierte Antwort
Star Strider
am 3 Jun. 2020
‘Why is it filtering the wrong frequencies in the middle bands?’
First, the sampling frequency of your signal and the sampling frequency of the filter are significantly different. They must be the same, or the filter will not work correctly with the signal.
Second, the Signal Processing Toolbox cannot read your comments, and cares not one whit about anything that is not in code that it can understand. It has absolutely no idea that you intend the frequencies to be in kHz.
Try this:
Fs = 28E+3; % Sampling Frequency
N = 48; % Order
F = [0 2 2.1 3.9 4 6 6.1 7.9 8 10 10.1 11.9 12 14]*1E+3; % Frequency Vector
A = [4 4 0 0 4 4 0 0 4 4 0 0 4 4]; % Amplitude Vector
W = [1 100 1 100 1 100 1]; % Weight Vector
% Calculate the coefficients using the FIRPM function.
b = firpm(N, F/(Fs/2), A, W);
figure
freqz(b, 1, 2^16, Fs)
Use filtfilt, not filter, to do the actual filtering.
2 Kommentare
Star Strider
am 3 Jun. 2020
As always, my pleasure!
I get entirely different results when I multiply the vectors and sampling frequency by 1000, as reflected in the freqz plot. I did not run all your code including the re-scaled filter, I just did the filter itself. I cannot imagine that the original and re-scaled filter produced the same results, unless filterDesigner scaled them itself from the original design specifications you gave it (that I have no access to), so that they work with a 28 kHz sampling frequency.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Digital Filter Analysis finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!