Filter signal with 2 different frequencies

I have an equation which gives 2 gaussian-sinc pulses of different frequencies, in dataset wrt time (t = 0:0.001:100). When put into an fft the two frequencies are found, but I want to design a filter for the signal to remove one of the pulses. How can I do this to the signal below for different values for f1 and f2?
f1 = 2.5, f2 = 1.5, s1 = 1.2/f1, s2 = 1.2/f2
D = cos(2*pi*f1*(t-10))*exp(-(t-10).^2/(2*s1^2)) + cos(2*pi*f2*(t-20))*exp(-(t-20).^2/(2*s2^2))

 Accepted Answer

Use either the bandpass or bandstop functions, depending on the result you want.
For best results, use the 'ImpulseResponse','iir' name-value pair.
.

4 Comments

I tried using both iir and fir bandpass filters but couldn't seem to make it work. What values exactly should I be using in it?
I have no idea what the problems with the bandpass function is, since I had problems getting it to work online here as well. I was able to get it to work with my command-line filters (that likely produce the same filters) —
Fs = 1000;
t = linspace(0, 50*Fs, 150*Fs)/Fs;
f1 = 2.5; f2 = 1.5; s1 = 1.2/f1; s2 = 1.2/f2;
D = cos(2*pi*f1*(t-10)).*exp(-(t-10).^2/(2*s1^2)) + cos(2*pi*f2*(t-20)).*exp(-(t-20).^2/(2*s2^2))
D = 1×150000
1.0e+00 * 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
figure
plot(t, D)
grid
xlabel('Time')
ylabel('Amplitude')
title('Original')
L = numel(t);
Fn = Fs/2;
NFFT = 2^nextpow2(L); % For Efficiency
FTD = fft(D - mean(D),NFFT)/L; % Fourier Transform
Fv = linspace(0, 1, NFFT/2-1)*Fn; % Frequency Vector (For Plots)
Iv = 1:numel(Fv); % Index Vector (For Plots)
figure
plot(Fv, abs(FTD(Iv))*2)
grid
xlim([0 1.5])
xlabel('Frequency')
ylabel('Magnitude')
Wp = [0.65 1.3]/Fn; % Define Passband In Hz, Normalise To (0,pi)
Ws = Wp.*[0.95 1.05]; % Stopband (Normalised)
Rs = 50; % Stopband Ripple (Attenuation) dB
Rp = 1; % Passband Ripple dB
[n,Wn] = ellipord(Wp,Ws,Rp,Rs); % Calculate Filter Order
[z,p,k] = ellip(n,Rp,Rs,Wp); % Design Filter
[sosH,gH] = zp2sos(z,p,k); % Implement Filter As Second-Order-Section Representation
figure
freqz(sosH, 2^16, Fs) % Filter Bode Plot
set(subplot(2,1,1), 'XLim',[0 5])
set(subplot(2,1,2), 'XLim',[0 5])
Wp = [0.2 0.65]/Fn; % Define Passband In Hz, Normalise To (0,pi)
Ws = Wp.*[0.95 1.05]; % Stopband (Normalised)
Rs = 50; % Stopband Ripple (Attenuation) dB
Rp = 1; % Passband Ripple dB
[n,Wn] = ellipord(Wp,Ws,Rp,Rs); % Calculate Filter Order
[z,p,k] = ellip(n,Rp,Rs,Wp); % Design Filter
[sosL,gL] = zp2sos(z,p,k); % Implement Filter As Second-Order-Section Representation
figure
freqz(sosL, 2^16, Fs) % Filter Bode Plot
set(subplot(2,1,1), 'XLim',[0 5])
set(subplot(2,1,2), 'XLim',[0 5])
D_FiltH = filtfilt(sosH,gH,D);
D_FiltL = filtfilt(sosL,gL,D);
D_Filt = [D_FiltL; D_FiltH]
D_Filt = 2×150000
-0.0012 -0.0011 -0.0011 -0.0011 -0.0011 -0.0011 -0.0011 -0.0011 -0.0011 -0.0010 -0.0010 -0.0010 -0.0010 -0.0010 -0.0010 -0.0010 -0.0010 -0.0009 -0.0009 -0.0009 -0.0009 -0.0009 -0.0009 -0.0009 -0.0009 -0.0009 -0.0008 -0.0008 -0.0008 -0.0008 -0.0004 -0.0004 -0.0004 -0.0005 -0.0005 -0.0005 -0.0005 -0.0005 -0.0005 -0.0005 -0.0005 -0.0005 -0.0005 -0.0005 -0.0006 -0.0006 -0.0006 -0.0006 -0.0006 -0.0006 -0.0006 -0.0006 -0.0006 -0.0006 -0.0006 -0.0007 -0.0007 -0.0007 -0.0007 -0.0007
figure
plot(t, D_Filt)
grid
xlabel('Time')
ylabel('Amplitude')
title('Filtered')
FTD_Filt = fft(D_Filt - mean(D_Filt,2),NFFT, 2)/L; % Fourier Transform
% D_FiltL = lowpass(D, 0.2, Fs, 'ImpulseResponse','iir')
% D_FiltH = highpass(D, 0.7, Fs, 'ImpulseResponse','iir')
% FTD_Filt = fft([D_FiltH; D_FiltL],NFFT,2)/L % Fourier Transform
figure
plot(Fv, abs(FTD_Filt(:,Iv))*2)
grid
xlim([0 1.5])
xlabel('Frequency')
ylabel('Magnitude')
There is some distortion in the results because some frequencies comomon to both functions were filtered out in each filter operation (seen most easily in the time-domain plot of the filtered signals).
.
That seems to have fixed it, thank you
As always, my pleasure!

Sign in to comment.

More Answers (0)

Products

Release

R2021b

Community Treasure Hunt

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

Start Hunting!