# Apply a bandpass filter in freq domain

63 views (last 30 days)
Commented: Star Strider on 10 Apr 2020
Hi. I am new to signal processing. Appreciate any help.
I have a signal with too many data points and high sampleing rate (500 MHz). The main goal is discarding the effect of the data outside a certain freq range (10- 100 kHz) for which our instrument is not calibrated. So I think I need to apply a bandpass filter in freq domain. I am not sure if I do that correctly, since after applying this filter, the magnitude of the data outside this range are still not zero and the filter doesn't discard the data outside range and it also changes the values wihtin the range. What is that I am doing wrong?
Here is the code:
fs = 500e6;
f1 = 10e3, f2 = 100e3;
wn = [f1/fs f2/fs];
[b,a] = butter(20,wn,'bandpass');
[H w] = freqz(b,a,length(x)); % x is the main signal
y = fft(x).*(H); % Applying the filter
Thank you

Star Strider on 6 Apr 2020
So I think I need to apply a bandpass filter in freq domain.
Don’t. That is extremely difficult, since the Fourier transform is symmetrical and it would be necessary to do that on the ‘positive’ as well as the ‘negative’ (complex-conjugate) parts, and so is generally not worth the effort.
fs = 500e6;
fn = fs/2;
f1 = 10e3, f2 = 100e3;
wn = [f1 f2]/fn;
[z,p,k] = butter(20,wn,'bandpass');
[sos,g] = zp2sos(z,p,k);
figure
freqz(sos,2^14,fs); % x is the main signal
set(subplot(2,1,1), 'XLim',[0 200E3]) % ‘Zoom’ Plot
set(subplot(2,1,2), 'XLim',[0 200E3]) % ‘Zoom’ Plot
x_filtered = filtfilt(sos,g,x);
The transfer-function implementation of that filter is demonstrably unstable. The second-order-section implementation is stable and will likely do what you want it to.
I suggest using a different filter design, perhaps an elliptic filter. See ellipord and ellip as well as zp2sos.

Star Strider on 7 Apr 2020
You’re asking the filters to do what is close to an impossible task.
I designed my own (elliptic) filters, and tried cascading highpass and lowpass filters as well as designinmg a bandpass filter. I cannot get either approach to work effectively with my test signal with those passbands.
Fs = 500E6;
Fn = Fs/2;
x = randn(1, 1E+7); % Create ‘x’
Wp = [10E3 100E3]/Fn; % Normalised Passband (Passband = 25 Hz To 75 Hz)
Ws = [ 7E3 150E3]/Fn; % Normalised Stopband (Passband = 20 Hz To 80 Hz)
Rp = 1; % Passband Ripple/Attenuation
Rs = 100; % Stopband Ripple/Attenuation
[n,Wp] = ellipord(Wp, Ws, Rp, Rs); % Calculate Elliptic Filter Optimum Order
[z,p,k] = ellip(n, Rp, Rs, Wp,'bandpass'); % Elliptic Filter
[sos,g] = zp2sos(z,p,k); % Second-Order-Section For Stability
x_filtered = filtfilt(sos, g, x);
figure
freqz(sos, 2^16, Fs)
set(subplot(2,1,1), 'XLim',[0 200E3]) % Zoom
set(subplot(2,1,2), 'XLim',[0 200E3]) % Zoom
L = numel(x); % Assumes x Is A Vector
FT_x = fft(x)/L;
FT_x_filtered = fft(x_filtered)/L;
Fv = linspace(0, 1, fix(L/2)+1)*Fn; % Frequency Vector
Iv = 1:numel(Fv); % Index Vector
TF = FT_x_filtered./FT_x; % Transfer Function
figure
semilogx(Fv, 20*log10(abs(TF(Iv))))
hold on
yl = ylim;
plot([1 1]*10E3, yl, '-r', 'LineWidth',1)
plot([1 1]*100E3, yl, '-r', 'LineWidth',1)
hold off
grid
I leave this for you to experiment with.
.
Thanks Star,
I experimenteted a lot and could get it to work. I ignored the bandpass and only applied a lowpass filter with second-order as you suggested. I accepted your original answer.
I have a few questions though:
1. So can those parameters such as Rs, Rp, Wp be any values? For instance, through reading online it is mentioned that 20<Rs<120 dB. So Rs can never be smaller than 20? or if the filter works for a smaller Rs value, that should be fine? (for me, Rs=20 worked).
2. The value of Ws and its distance from f2 (i.e., the length of transition region) should also be among a certain range? Or whatever values works is fine? I mean theoritically the transtion region should be as small as possible? or it doesn't mater?
3. Is there a rule of thumb for Ws, something like 10% more than f2?
Thanks again so much for your help
Star Strider on 10 Apr 2020
As always, my pleasure! Thank you!
1. The ‘Rs’ and ‘Rp’ are are the stopband and passband attenuations, so ‘Rs’ must always be greater than ‘Rp’ if the filter is to be effective. I do not recall any specific limits on ‘Rs’. (I usually use 50 for ‘Rs’, however there is no specific value for it.) The ‘Wp’ parameter is the passband edge, and defines the filter. (It, in combination with ‘Ws’ define the transition region.)
2. The relation between ‘Ws’ and ‘Wp’ depend on the filter design and where they are in the spectrum, so in a highpass or bandpass filter this is more of a problem if they are close to 0 than if they are higher in the spectrum. (In that instance, I usually choose ‘Ws’ that is half the value of ‘Wp’ for highpass filters or the lower edge of a bandpass filter.) As a general rule, it is best to avoid short/steep transition regions because this will increase filter complexity and can lead to instability. I generally keep the transition regions to about 5% of the passband edges. This depends on the filter, and the results of preliminary experiments with the filter and the particular signal.
3. The 10% criterion works, however I tend to prefer symmetry, so the same transition regions on both sides of a bandpass or stopband filter. It also depends on the filter, so elliptic filters are more likely to be stable with small transition regions than Butterworth designs.
Again, my pleasure!