Cihan Keser
Cihan Keser

Reputation: 3261

How to make a Simple FIR Filter using Matlab?

How can I make a simple low-pass FIR filter using Matlab (without using the built-in function) ?

Problem example:

Implement a FIR LPF with cut-off frequency 250Hz

it may also be necessary that, sampling freq is given...

Solution attempt or what I already know:

x = [...] -> input signal
A = 1; -> Since this is FIR
B = [?????]
y = filter(B, A, x) -> Output signal

Afaik, B should contain the coefficients for the FIR filter. But; how do I calculate these coefficients given that I only have the cut-off frequency?

Upvotes: 5

Views: 26396

Answers (3)

Jotorious
Jotorious

Reputation: 185

Since the Coefficients to a LTI filter are the time domain impulse response, you could create the frequency response in matlab by specifying an Amplitude vector, and and Phase Vector, then Inverse FFT them to get your coefficients, for example, soemthing like A = [ 1 .9 .8 .5 .2 .1 0], theta=[0 0 0 0 0 0 0], then H=A.*exp(j*theta) then coefs = ifft(H)

Upvotes: 0

S.C. Madsen
S.C. Madsen

Reputation: 5256

If you wan't a different shape of the amplitude spectrum, do exactly as sellibitze suggested, only replace the sinc function with the real part of the inverse Fourier-transform of the desired amplitude response (delayed to get causal symmetrical impulse response).

Upvotes: 0

sellibitze
sellibitze

Reputation: 28107

The simplest thing is a "windowed sinc" filter:

fs = 44100;
cutoff = 250;
t = -256:256;  % This will be a 513-tap filter
r = 2*cutoff/fs;
B = sinc(r*t).*r .* blackman(length(t))';
freqz(B);

The length of the filter (see t=...) controls the width of the transition band. cutoff is in this case the -6 dB point. blackman is the name of a popular window. You can check out this Wikipedia page for more infos on window functions. They basically have different trade-offs between transition band width and stopband rejection.

Upvotes: 11

Related Questions