College of Engineering and Computer Science
ELEC2020 - Signals and Information
Spring 2025
LAB REPORT 4: Sampling, Convolution, and FIR Filtering
Members: Date: 6th May, 2025
Member 1: Le Quang Nhat Lab Instructor: Professor Nidal Kamel
Member 2: Duong Tuan Manh Lab Assistant: Tran Trung Dung
Lab Excercise
3.1 Deconvolution Experiment for 1-D Filters
We are analyzing a finite impulse response (FIR) filter characterized by the following difference equation:
w[n] = x[n] − 0.9 x[n − 1]
In this setup, the input signal x[n] consists of a recurring pulse pattern. This signal can be generated using
MATLAB with the following implementation:
1 n = 0:100;
2 xx = 256 * ( rem (n , 50) < 10) ;
To implement the filter in MATLAB:
1 bb = [1 , -0.9];
2 ww = firfilt ( bb , xx ) ;
(a) Plotting x[n] and w[n]
We display the input and output over the range 0 ≤ n ≤ 75:
1 subplot (2 ,1 ,1) ;
2 stem (n , xx , ’ filled ’) ;
3 title ( ’ Input Signal x [ n ] ’) ;
4 subplot (2 ,1 ,2) ;
5 stem (0: length ( ww ) -1 , ww , ’ filled ’) ;
6 title ( ’ Output Signal w [ n ] = x [ n ] - 0.9 x [n -1] ’) ;
1
College of Engineering and Computer Science
ELEC2020 - Signals and Information
Spring 2025
Figure 1: Spectrogram of Aliased Chirp Signal
(b) Signal Length Analysis
• Input signal length: length(x[n]) = 101
• Filter length: 2
• Output signal length: length(w[n]) = 102
This result aligns with the general formula:
length(w[n]) = length(x[n]) + length(bb) − 1
The filter functions as a first-difference operator, highlighting transitions in the signal x[n], such as rising
and falling edges.
3.1.1 Restoration Filter
To reverse the effect of the filter w[n] = x[n] − 0.9x[n − 1], we apply another FIR filter defined by:
2
College of Engineering and Computer Science
ELEC2020 - Signals and Information
Spring 2025
22
X
y[n] = rℓ w[n − ℓ], r = 0.9
ℓ=0
1 r = 0.9;
2 M = 22;
3 b2 = r .^(0: M ) ; % Filter -2 coefficients
4 y = firfilt ( b2 , w ) ; % Restoration filtering
(a)-(b) Plotting Results
We compare the filtered signal w[n] and restored signal y[n]:
Figure 2: Top: Signal w[n] after first filter. Bottom: Restored signal y[n] after second FIR filter.
(c) Error Signal
We compute the error e[n] = x[n] − y[n] and plot it for 0 ≤ n ≤ 50:
1 e = x - y (1: length ( x ) ) ;
2 stem (0:50 , e (1:51) , ’ filled ’) ;
3
College of Engineering and Computer Science
ELEC2020 - Signals and Information
Spring 2025
Figure 3: Error between x[n] and restored y[n] for 0 ≤ n ≤ 50.
3.1.2 Worst-Case Error
(a) Computation
We compute the maximum absolute error over the interval 0 ≤ n ≤ 50 using the following MATLAB
command:
worst_error = max(abs(e(1:51)));
Result: The worst-case error is approximately 6.8357.
(b) Interpretation
The error is relatively minor compared to the signal amplitude (256), indicating that the restoration process
is reasonably effective. However, some discrepancies remain due to:
• The restoration filter is only an approximate inverse.
• The filter has a finite length (M = 22), rather than being an ideal infinite impulse response filter.
To ensure the error is visually negligible on a plot, the worst-case error should ideally be less than 1% of the
signal’s amplitude—i.e., below 2–3 in this case.
3.1.3 An Echo Filter
We create an echo effect using an FIR filter described by the equation:
y[n] = x[n] + r · x[n − P ]
4
College of Engineering and Computer Science
ELEC2020 - Signals and Information
Spring 2025
(a) Parameter Selection
• Sampling rate: fs = 8000 Hz
• Desired delay for the echo: 0.2 seconds, leading to P = 8000 × 0.2 = 1600 samples
• Echo amplitude: r = 0.9
(b) Filter Coefficients
This FIR filter has the following impulse response:
h[n] = δ[n] + r · δ[n − P ]
This can be implemented in MATLAB as:
bb = [1, zeros(1, P-1), r]; % Length = P + 1 = 1601
(c) Implementation
Apply the filter to the speech signal x2:
1 y_echo = filter ( bb , 1 , x2 ) ;
2 soundsc ( y_echo , 8000) ; % Listen to the echoed result
Figure 4: Top: Original signal x[n] (from x2). Bottom: Echoed signal y[n] with P = 1600, r = 0.9.
5
College of Engineering and Computer Science
ELEC2020 - Signals and Information
Spring 2025
3.2 Cascading Two Systems
3.2.1 Overall Impulse Response
(a) MATLAB Code to Implement Cascaded System:
1 % Parameters
2 q = 0.9;
3 r = 0.9;
4 M = 22;
5
6 % Input : delta [ n ]
7 x = [1 zeros (1 , 100) ]; % Impulse input
8
9 % First FIR Filter : w [ n ] = x [ n ] - q * x [n -1]
10 h1 = [1 -q ];
11 w = firfilt ( h1 , x ) ; % w[n]
12
13 % Second FIR Filter : y [ n ] = sum_ { l =0}^ M r ^ l * w [ n - l ]
14 h2 = r .^ (0: M ) ; % This is r ^ l for l = 0 to M
15 y = firfilt ( h2 , w ) ; % Final output y [ n ]
16
17 % Plot impulse response of overall system
18 n = 0: length ( y ) -1;
19 stem (n , y , ’ filled ’)
20 xlabel ( ’n ’)
21 ylabel ( ’h [ n ] = y [ n ] ’)
22 title ( ’ Impulse Response of Cascaded FIR System ’)
23 grid on
Figure 5: Graph 3.2.1
(b) Hand Verification
To verify the correctness of the cascaded FIR system, we manually compute its overall impulse response:
h[n] = h1 [n] ∗ h2 [n]
6
College of Engineering and Computer Science
ELEC2020 - Signals and Information
Spring 2025
where
h1 [n] = δ[n] − q δ[n − 1], h2 [n] = {r0 , r1 , r2 , . . . , rM }
The convolution of two discrete-time signals is defined as:
∞
X
h[n] = h1 [k] · h2 [n − k]
k=−∞
Since both h1 [n] and h2 [n] are finite-duration sequences, the expression simplifies to:
h[n] = h1 [0] · h2 [n] + h1 [1] · h2 [n − 1]
Substituting known values:
h[n] = rn − q · rn−1 , for n = 1, 2, . . . , M + 1
h[0] = r0 = 1
Thus, the overall impulse response is given by:
1,
n=0
h[n] = r n−1
(r − q), 1 ≤ n ≤ M + 1
0, otherwise
This analytical result matches the output computed in MATLAB using conv() or filtfilt(). Notably,
when q = r, we obtain h[n] = δ[n], implying that the cascaded system perfectly reconstructs the original
signal x[n], achieving ideal deconvolution.
(c) Perfect Deconvolution Condition:
In deconvolution scenarios, FIR FILTER-2 is designed to reverse the effects of FIR FILTER-1. Perfect
deconvolution occurs when:
h1 [n] ∗ h2 [n] = δ[n]
That is, the cascade must behave like an identity system, fully recovering the input x[n].
This concludes the analysis of the overall impulse response in the cascaded FIR filter system.
3.2.2 Distorting and Restoring Images
(a) Load the Image
1 load echart . mat % Loads ’ echart ’ grayscale image
2 imshow ( echart , []) ; title ( ’ Original Image : echart ’) ;
7
College of Engineering and Computer Science
ELEC2020 - Signals and Information
Spring 2025
(b) Apply FIR FILTER-1 (Distortion)
1 q = 0.9;
2 bdiffh = [1 , -q ]; % First - difference filter
3
4 % Horizontal filtering
5 ech_temp = conv2 ( echart , bdiffh , ’ same ’) ;
6
7 % Vertical filtering
8 ech90 = conv2 ( ech_temp , bdiffh ’ , ’ same ’) ;
9
10 imshow ( ech90 , []) ; title ( ’ Distorted Image ( ech90 ) ’) ;
(c) Apply FIR FILTER-2 (Restoration)
1 r = 0.9; M = 22;
2 b_restore = r .^ (0: M ) ;
3
4 % Horizontal and vertical filtering
5 temp_restored = conv2 ( ech90 , b_restore , ’ same ’) ;
6 ech_restored = conv2 ( temp_restored , b_restore ’ , ’ same ’) ;
7
8 imshow ( ech_restored , []) ; title ( ’ Restored Image ’) ;
Explanation: The filter causes “ghosts” (echo-like shadows) near high contrast regions due to FIR FILTER-
1 distortion. FIR FILTER-2 partially reverses it using integration-like behavior.
Worst-case Error
1 diff = double ( echart ) - ech_restored ;
2 worst_error = max ( abs ( diff (:) ) ) ;
3 disp ([ ’ Worst - case error : ’ , num2str ( worst_error ) ]) ;
Worst-case error: It quantifies maximum deviation from the original, especially at black-white transitions.
3.2.3 A Second Restoration Experiment
(a) Try Different Values of M
1 M_values = [11 , 22 , 33];
2 r = 0.9;
3
4 for M = M_values
5 b_restore = r .^ (0: M ) ;
6 temp_restored = conv2 ( ech90 , b_restore , ’ same ’) ;
7 e c h _ r e s t o r e _ t r i a l = conv2 ( temp_restored , b_restore ’ , ’ same ’) ;
8
9 figure ;
10 imshow ( ech_restore_trial , []) ;
11 title ([ ’ Restored with M = ’ , num2str ( M ) ]) ;
12 end
Best M: The best value of M balances between minimal ghosting and preserving sharp edges. M = 22
often works well visually.
8
College of Engineering and Computer Science
ELEC2020 - Signals and Information
Spring 2025
(b) Gray-Level Error
1 g r a y _ e r r o r _ l e v e l s = worst_error / 256;
2 disp ([ ’ Max error in gray levels : ’ , num2str ( g r a y _ e r r o r _ l e v e l s ) ]) ;
Interpretation: If the error is less than 1 gray level (1/256), humans typically cannot detect it. If so, the
restoration is visually perfect.
Conclusion: Restoration using FIR FILTER-2 is approximate, but a well-chosen M can suppress visual
artifacts effectively.
Original and Distorted Image for 3.2.2 and 3.2.3
Figure 6: Original Image from echart.mat
9
College of Engineering and Computer Science
ELEC2020 - Signals and Information
Spring 2025
Figure 7: Restored image in part 3.2.2c
10
College of Engineering and Computer Science
ELEC2020 - Signals and Information
Spring 2025
Figure 8: Distorted Image ech90 after applying FILTER-1 with q = 0.9
Restoration using FIR FILTER-2
We now apply FIR FILTER-2 with r = 0.9 and various values of M (11, 22, 33) to try and undo the
distortions.
Figure 9: Restored Image with FILTER-2, M = 11 − 33
Comparison and Best Choice
Upon comparing the three restoration results, we observe the following:
• M = 11: Restoration is incomplete; noticeable ghosting persists.
• M = 22: Offers a good balance; ghosting is substantially reduced.
11
College of Engineering and Computer Science
ELEC2020 - Signals and Information
Spring 2025
• M = 33: Slight overshoot around edges; minor shadow artifacts begin to reappear.
From visual analysis, the case with M = 22 yields the most favorable trade-off between enhancing edge
sharpness and suppressing echo-related distortions.
12