ubuntu2004
Lab session VI : Finite Impulse Response (FIR) Filters
Introduction
Up to now, we have focused our attention on signals and their mathematical representations. In this lab, we begin to emphasize systems or filters. In signal processing, a filter is a system (usually a linear and time-invariant system - a SLIT) which transforms an input signal in an output signal with modified characteristics. The general concepts of linearity and time invariance completely characterize a wide class of systems that are exceedingly important in both the continuous-time and the discrete-time cases.
Strictly speaking, is a system that is designed to remove some component or modify some characteristic of a signal, but often the two terms (filter and system) are used interchangeably.
In this lab, we introduce the class of finite impulse response (FIR) systems, or, as we often refer to them, FIR filters. These filters are systems for which each output value is the sum of a finite number of weighted values of the input sequence. We define the basic input–output structure of the FIR filter as a time-domain computation based upon what is often called a difference equation.
For linear and time-invariant systems, the output signal is the result of the convolution between the input signal and the impulse response of the system. The unit impulse response of the filter is defined and shown to completely describe the filter via the operation of convolution.
For digital systems, with x[n] and b[n] are respectively the input signal and the impulse response of the system, sampled at a frequency , this convolution takes the form : In the following, we will be dealing with finite impulse response (FIR) filters, for which this equation reduces to a simplified, finite difference equation, where M~1~ and M~2~ are positive integers : ParseError: KaTeX parse error: Undefined control sequence: \displaylines at position 2: \̲d̲i̲s̲p̲l̲a̲y̲l̲i̲n̲e̲s̲{ y[n] = \sum… We will also restrict to causal filters, which means that we will only use past values of the input (x[n-m] with m>0) to compute the current value of the output y[n], so that this difference equation further reduces to the simple form : ParseError: KaTeX parse error: Undefined control sequence: \displaylines at position 2: \̲d̲i̲s̲p̲l̲a̲y̲l̲i̲n̲e̲s̲{ y[n] = \sum… where M~2~ is the order of the causal FIR filter, which is thus completely determined by the values of the coefficients b[m] of its impulse response. This difference equation tells us how to compute each sample of the output signal y[n] as a weighted sum of samples of the present and past values of the input signal x[n].
Only causal filters can be implemented in real time, non-causal filters require the storage of all the values of the input signal beforehand, the filtering processes being performed “afterwards”, i.e. already knowing the entire original signal before processing.
Problem 6.1 : Implementing the running convolution algorithm
In a first step, you will modify the running correlation function that you have developed in a previous lab. Convolution is very close to correlation, it only differs by a mere time inversion on one of the second signal. Therefore, all you have to do is to invert the elements of the reference signal y[n] and to remove the complex conjugation.
Correlation | Convolution |
---|---|
By adding this inversion, you will easily transform correl(x,y) in convol(x,y). In the following exercise, you will verify that the convolution with a gaussian corresponds to a sliding mean, which introduces some blurr in the x signal.
Gaussian function : Write a new function mygauss(x,mu,var) which generates a vector y with the elements of a gaussian of mean mu (m) and variance var () for all the values of the input x, using the bijection . Using this function, generate a vector y on the domain [0,50] containing a sampled gaussian centered on and of , and plot it in the top graph of a figure containing three graphs (at the end).
Signal x : Loads the contents of the data file Lab6_data.mat into your Octave environment. You should have loaded the array sunspot, which holds in its second column the mean integrated intensity of the solar sunspots for the last 300 years, with respect to the year (in column 1). Linked to solar activity, these sunspots exhibit a peak intensity approximately every eleven years, as you could verify using an autocorrelation or the DFT (the subject of next lab).
Build a vector x with the sunspot intensities of the second line of sunspot, plot these intensity variations in the central graph of the figure.
Convolution between x and y : Use your newborn function convol to compute the (discrete) convolution between x and y, and plot the result in the bottom graph.
Effect of the Gaussian width : Calculate the convolution between x and y for several values of the variance, , then .
warning: implicit conversion from numeric to char
Problem 6.2 : Extraction of a sinusoidal component using selective FIR filtering
You will apply a purpose-built FIR filter to extract a particular sinusoid at the frequency from a signal affected by noise and which contains other sinusoids at various frequencies.
The main question is how to choose the right impulse response to get as an output of the FIR filter the expected sinusoid at the right frequency , supposed to be effectively present in the input signal. Take a moment to think about it : Remember that convolution is close to correlation, itself a sliding scalar product…
After some thoughts, it seems logical that the impulse response should be similar to the expected output signal. We will thus use a trial impulse response in the form of : where M is the order of the FIR filter
Signal x : On a one second domain, generate the signal x[n] made of the sum of two sinusoids of the same amplitude 1 at frequencies and , using a sampling frequency such that x[n] contains points.
fs = 1024
T = 1
Impulse response of the FIR filter : Keeping the same sampling frequency, generate the coefficients of the desired impulse response, taking in a first step .
Signal filtering : Use the function convol, to realize the FIR filtering of x[n] using as y a vector b containing the coefficients of the FIR filter determined by the above expression.
Results : In a three graph figure, plot the signal x[n], the filter coefficients b[k], and finally the filtered signal x_filt. What is the apparent frequency of the filtered signal ? (Suggestion : for a precise determination use your function apfc or the DFT).
n_x = 1024
n_b = 129
f0 = 9.7778
Visualization of the FIR impulse response : The instruction
[H,w]=freqz(b,1,nu];
furnishes the frequency response over n points for a discrete pulsation between 0 and , thus a discrete (normalized) frequency between 0 and , the Nyquist frequency (remember that the discrete frequency of a discrete signal is given by ). To get back to usual (continuous) frequencies, you can replace nu by a vector containing explicitely the values of the discrete pulsation for which you want to know H(w). For example, the instructionH=freqz(b,1,[f0 f1]*2*pi/fe);
returns a doublet with the values of H at and at . In a two-graph figure, plot modulus and phase of the frequency response H(f) of your FIR filter in a frequency domain between 0 and 100 Hz.
0## 6. Rejection factor : The quantity defined by gives a simple criterion to appreciate the selectivity of a filter, its ability to select more or less the right sinusoid. Filters with high R will be more effective in discriminating between sinusoidal components at frequencies and , e.g. 10 and 15 Hz. In the preceding figure, add a stem to identify on the modulus |H(f)| the two values and , and computes the corresponding rejection ratio.
H =
8.139 - 64.500i -26.513 + 14.177i
R = 2.1623
Optimization of the order M of the FIR filter : Compare the modulus corresponding to various input responses with for n=4 up to 9, and compute the corresponding rejection ratios. Plot these ratios w.r.t. n in a multi-graph figure, which will also contain the modulus of the various frequency responses and the stem on the particular values and .
N = 2048
warning: implicit conversion from numeric to char
R_arr =
1.0324
1.1522
1.2306
2.1079
6.9393
9.8136
Food for thought : A clever way to increase the rejection ratio without increasing the length of b[n] would be to localize in the first zero of the sinc. Find the ideal order M fulfilling this goal.
M = 205.80
R_arr = 2.0849e+04
The sinc is coming from the rect window in the time domain. we know the width of the rect is the inverse of the first 0 of the sinc (property of fourier transform) Since our sinc in frequemcy space is centered around 10 and we want the sinc to be 0 at 15 we need b to be windowed by a rect with the width of 1/5s This gives an R value around 2000 which is way higher than we what we saw for bigger M values before
Robustness of the FIR filter : Taking , test the filter capability to operate in a noisy environment, by adding to the signal x[n] some random additive gaussian noise.
N = 1024
n_x = 1024
n_b = 257
warning: implicit conversion from numeric to char
Problem 6.3 : "Off-line" 1D-signal filtering with GNU Octave/MatLab
Introduction
Although we know how to do it using running convolution, It is not always necessary to filter signals in real time. Signals can be stored in computer memory, then they may be processed and filtered a posteriori ("Off-line" filtering), as is often the case for images and 2D-filtering.
Before going further, please read carefully the pages "Additional notes for Lab session VI : Finite Impulse Response (FIR) Filters".
Noise reduction in 1D signals
From the data file Lab6_data.mat, the signal “simple” should be available, a noise-free 1D signal, as well as “simple_noise”, the same signal corrupted by a random noise.
Effect of delay :
Let’s examine the delay introduced by the two filtering implementations, filter and filter2. Filter simple with a 7-point running average filter. Do this twice, first using filter and then using filter2 with the “same” parameter[1]. Pay attention to the fact that simple is a column vector.
In a figure with three graphs, plot ( stem ) the original and the two filtered signals.
One of the filtering commands introduce some delay. Which one ? How many samples of delays have been introduced ?
Compute the RMS error between the original and the two filtered signal. Which one is lower ? Why ?
[1] Use the “same” parameter every time you filter a signal using filter2 in the remainder of this lab.
tmp = 0
index_min_s = 40
tmp = 4.7143
index_min_f = 43
tmp = 4.7143
index_min_f2 = 40
out = 12.662
ans = 12.662
out = 6.6065
ans = 6.6065
Running 1D average filters
1.Use again filter2 to apply a 3-point, a 5-point, and a 9-point moving average filter to the noisy signal simple_noise.
2.Use two subplot instructions to plot the original signal, the three filtered signals, and the three sets of filter coefficients in seven panels of a single figure (use plot for the signals and stem for the coefficients).
3.Compute the RMS error between each filtered signal and simple.
4.Which one of the four moving-average filters that you have applied has the lowest RMS error
“Tapered” smoothing 1D filters
Use the file g_smooth.m (provided in the file "Lab6_Material.zip") to generate filter coefficients with “widths” of 0.5, 0.75 and 1.0. Note the lengths of the returned coefficient vectors, plot them in a single figure to get a sense of how the “width” factor affects them. Use filter2 to apply these filters to simple_noise.
Plot the three filtered signals and the three sets of coefficients in a figure with six panels.
Compute the RMS error between simple and each of the filtered signals.
Which filter has the lowest RMS error ?
ans = 3.8642
ans = 4.1171
ans = 4.6322
Non-linear filtering of a spiky signal
Generate a “spiky” signal starting from the signal simple by replacing its current values by 250 at the indices n=20, 60, and 90.
Filter the spiky signal using a 1D tapered smoothing filter with a width of 0.5.
Filter the spike signal using a non-linear median filter with N=5, and plot the three signals in a single figure.
Differentiation and edge-finding of 1D signals
Applying a difference filter
Use a differentiating filter characterized by to the signal simple. Plot the filtered signal, find the location of the non-zero “features” of this signal, and make the link with what they correspond to in the original signal.
diff_filt = [-1,1]; filtered_diff = filter(diff_filt,simple); stem(filtered_diff)
Conclusions
Complete this cell with your personal conclusions about what you've learned. Give an estimation on the time you've spent (outside the alloted time for lab sessions) realizing this lab session.