What is wrong with my filter?

조회 수: 5 (최근 30일)
Mantas Kirna
Mantas Kirna 2020년 6월 3일
댓글: Star Strider 2020년 6월 3일
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')

채택된 답변

Star Strider
Star Strider 2020년 6월 3일
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
Mantas Kirna
Mantas Kirna 2020년 6월 3일
편집: Mantas Kirna 2020년 6월 3일
It works!
The diference in sampling frequency was the problem.
The code of the filter was generated by MATLABs filter designer in kHz if I multiply my values by a 1000 it makes no diference. I have no clue how that works :D
Thank you so much!
Star Strider
Star Strider 2020년 6월 3일
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.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Digital Filter Analysis에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by