design a low pass filter using the window method but do not apply the inbuilt window functions, create the window manually as well in matlab

To design a low pass filter with the window method, we should follow the steps below:

  1. Determine the filter specifications, like the filter order, cutoff frequency, sampling frequency, etc.

  2. Choose a window function that satisfies the specifications, such as the Hamming, Blackman, or Kaiser windows.

  3. Generate the window function, which is defined as a vector of length equal to the filter order

  4. Compute the filter coefficients by multiplying the impulse response of an ideal low-pass filter by the window function.

  5. Apply the filter coefficients to the input signal using the 'filter' function in MATLAB.

Here's an example code that demonstrates the process described above using a rectangular window:

main.m
%% Input specifications
Fs = 1000;           % Sampling frequency
Fc = 100;            % Cutoff frequency
N = 100;             % Filter order

%% Compute normalized frequencies
wc = 2*pi*Fc/Fs;     
w = linspace(0, pi, N+1);

%% Ideal lowpass filter
Hd = sinc(wc/pi*(0:N));

%% Rectangular window
w_rect = ones(N+1, 1);

%% Compute the filter coefficients
h = Hd.' .* w_rect;

%% Apply the filter to a test signal
x = randn(1, 1000);  % Generate random input signal
y = filter(h, 1, x); % Apply the filter

%% Plot the frequency response and impulse response
freqz(h, 1, 512, Fs);
title('Frequency Response');

figure;
stem(0:N, h);
title('Impulse Response');
660 chars
30 lines

Note that in the above code, we manually created a rectangular window using a vector of ones. You can replace this window with any other window function that satisfies the filter specifications.

related categories

gistlibby LogSnag