# How do I use this bandpass filter? [closed]

I found this free DSP book by Rutgers University professor Sophocles J. Orfanidis. The book is called Introduction to Signal Processing and the link contains many different implementations of common DSP functions in both MATLAB and C.

My intention is to eventually implement this Butterworth BPF in C for an embedded platform. The BPF is described in section 11.6.4 on page 618 of the PDF version of this book.

On page 622 there is a filter design example 11.6.5 which shows how to obtain the BPF coefficients. As I understand it, these coefficients are for the four stages of a cascaded filter design, $$H_0(z), H_1(z), H_2(z), H_3(z)$$ as described on page 621 and the overall transfer function is $$H(z) = H_0(z)H_1(z)H_2(z)H_3(z)$$

I do not understand how to use the output of bpsbutt.m to create $$H(z)$$ and $$h(n)$$

My goal is to generate band-limited noise on an embedded platform in real time. I want to do this by generating AWGN and filtering it with a BPF. Platform i am using is an RFSoC with an ARM processor.

The center frequency must be adjustable across a range from 500 MHz to 2000 MHz with a minimum center frequency step of 1 MHz and a maximum of 50 MHz.

The bandwidth of the noise must be adjustable from 100 MHz to 300 MHz.

My question is this: Could you please provide a MATLAB example of how to properly use this function to filter a signal?

• Hi! You're asking for matlab code, written to your specification! That's explicitly off-topic here. Instead, try to explain the signal processing problem (not the programming problem) that you're trying to solve – why not simply use matlab's own filter design methods? also: are you sure you want to do a butterworth on an embedded device? Filter designs lifted from the analog domain to the discrete-time domain are rarely the optimal choice. Commented Aug 18, 2022 at 7:30
• Thank you for replying! I am not sure and thats why im asking questions here. What do you suggest as an alternative? The problem i am trying to solve is to generate band limited noise and my approach is to generate AWGN then filter with a BPF to get band-limited noise. I need the BPF to be tunable and to be able to operate across a frequency range Commented Aug 18, 2022 at 13:37
• then: don't use a variable bandpass filter (which is really annoying to re-design on the fly, especially on a small computer), but a fixed-bandwidth low-pass filter. Multiply its output with a cosine (or a complex sinusoid, if this is complex baseband) to shift the center frequency from 0 to the desired frequency. Commented Aug 18, 2022 at 13:43
• I need to be able to adjust the width of the passband of the filter as well not just the center frequency. Generating coefficients is not a problem with the embedded device i am using. It has more than enough resources to be able to do this. Commented Aug 18, 2022 at 13:45
• you can totally forget about creating four billion samples of white noise on the ARM. Does, and will not happen. Source: supervised a FPGA thesis doing gigasample WGN generation on an RFSoC. You're multiple orders of magnitude off in performance from what you need. And I'm talking about hand-assembled highly optimized noise generation, not about matlab. Commented Aug 18, 2022 at 14:42

That is one of the more tortured output formats I've seen: It's basically cascaded 4th order sections. For embedded implementation I would recommend converting this into cascaded 2nd order sections. If you want to use this format directly in Matlab you can try something like this:

%% Design bandpass from 1kHz to 2kHz,
fs = 48000; % sample rate
[a,b,p] = bpsbutt(1,fs,1000,2000,1000/sqrt(2),2000*sqrt(2),1,60);
% calcualte the impulse response by cascaded filtering
nx = 2^14; d0 = zeros(nx,1); d0(1) = 1;
h0 = d0;
for i = 1:size(a,1)
h0 = filter(b(i,:),a(i,:),h0);
end

%% do a quick plot
fh0 = fft(h0);
frAxis = (1:nx/2)'/nx*fs;
semilogx(frAxis,20*log10(abs(fh0(2:nx/2+1))));
xlabel('Freq in Hz ->');
ylabel('Level in dB ->');
grid('on');
set(gca,'ylim',[-100 3]);

• thank you for replying! This is really helpful! My goal is to generate band-limited noise on an embedded device and my approach is to create AWGN samples and filter them with a BPF that is tunable and can operate across a frequency range. Could you point me to some literature that would be better suited for this application if you think this is not the best approach? Commented Aug 18, 2022 at 13:39
• Matlab and Python have perfectly good filter design packages build in. Commented Aug 18, 2022 at 16:22

The MATLAB code you have shown just outputs coefficients for IIR butterworth filter, the same can be done with MATLAB's built in butter function. It's probably not required that you implement this into embedded unless absolutely necessary. There's plenty of documentation on how to find the coefficients for the type of filter you want (i.e. butterworth) depending on your application:

https://uk.mathworks.com/help/signal/ref/butter.html

The only thing useful MATLAB is probably useful for is quickly calculating the coefficients for your filter offline and hard-code them in your embedded code.

The butterworth filter you have shown is an IIR filter as it provides two sets of coefficients a and b. The link you have provided does not have a .c implementation of such filter (only see a FIR.c implementation). You can search online for implementing IIR filters in C ; example here on stack overflow:

https://stackoverflow.com/questions/50588879/how-to-implement-iir-filter-in-c

• Thanks for replying. I know the link doesnt have a C implementation thats why i said i want to implement it in C and yes it is necessary for me. I need to generate the coefficients because the filter needs to be tunable and be able to operate across a frequency band so hard coding coefficients is not something i can do. Commented Aug 18, 2022 at 13:34
• Are you saying you NEED to compute the filter coefficients in real time on embedded hardware? May I ask why?
– Jdip
Commented Aug 18, 2022 at 13:36
• Because i need to be able to change my center frequency and bandwidth of operation Commented Aug 18, 2022 at 13:43
• I guess it depends on which embedded processor you intend to do this on - If it's anything like microchip, they have their own library code for applying IIR and FIR filter but no libraries for generating coefficients; other platforms may be more generous. Commented Aug 18, 2022 at 13:55
• its a Xilinx Zynq RFSoC Commented Aug 18, 2022 at 14:20