Code Walkthrough
Learn how to design FIR filters in Scilab using the wfir function. This script generates coefficients, applies convolution, and visualizes the results.
Signal Initialization
clc;
clear;
// Define time vector and composite signal
t = 0:0.01:1;
x = sin(2*%pi*5*t) + sin(2*%pi*50*t);
We define a sampling period of 0.01s (100 Hz). The signal x contains a 5 Hz component (desired for LPF) and a 50 Hz component (desired for HPF).
FIR Filter Design (wfir)
// Design Low Pass and High Pass filters
n = 20; // Filter order
fc = 0.1; // Normalized cutoff freq (5Hz)
h_lp = wfir('lp', n, fc);
h_hp = wfir('hp', n, fc);
The wfir function is Scilab's built-in tool for windowed FIR design. We specify 'lp' for Low Pass and 'hp' for High Pass. The cutoff fc=0.1 represents 10% of the Nyquist frequency.
Time Domain Convolution
// Apply filters using convolution
y_lp = conv(x, h_lp);
y_hp = conv(x, h_hp);
We use conv to compute the linear convolution between our signal x and the impulse response h. This is the fundamental operation of an FIR filter in the time domain.
Visualizing the Output
// Plotting results
subplot(3,1,1); plot(t, x); title("Input Signal");
subplot(3,1,2); plot(y_lp); title("LPF Output (5Hz passed)");
subplot(3,1,3); plot(y_hp); title("HPF Output (50Hz passed)");
The plots allow us to verify the filtering. The LPF result will show a smooth 5 Hz sine wave, while the HPF result will show the faster 50 Hz oscillations with the low frequency removed.