VIETNAM NATIONAL UNIVERSITY – HO CHI MINH CITY INTERNATIONAL UNIVERSITY SCHOOL OF COMPUTER SCIENCE AND ENGINNERING Digital Signal Processing LaboratoryTRANSFER FUNCTION AND DIGITAL FILTE
Fourier Transform
The Fourier transform is a mathematical function that decomposes a time-dependent waveform into frequencies The transform's output is a complex-valued frequency function In simpler words, the Fourier transform can help find the frequencies of a function in the time domain [1]
For doing the Fourier transform, we will use the following formula [2] :
Applying the Fourier transform to the square wave reveals its hidden frequency components.
It shows a series of vertical lines on a frequency axis, representing the different frequencies present in the signal Here is what it may look like:
Figure 1:Example of Fourier transform (taken from” Why Fourier series and transform of a square wave are different? - Signal Processing Stack Exchange”
Fourier transform has numerous applications in real-life and here are some of them:
Transfer Function
A transfer function is defined as the ratio of the output Laplace transform to the input Laplace transform, assuming that all initial conditions are zero [3] It consists of seven different types: a) Impulse response b) Difference equation c) I/O Difference Equation d) Frequency response e) Block diagram of realization f) Sample Processing Algorithm g) Frequency Response and Pole/zero Pattern
We have the direct form to do the transfer function which is:
Here by taking the z-transform of both sides of the equation we get:
Transfer function can have many applications in real-life Here are some of them:
Resonator Filter
A resonator filter, also known as a resonant filter or a resonant circuit, is a form of electrical filter that filters or amplifies certain frequencies using the idea of resonance.
Resonator filters can be implemented using various electronic components and circuit configurations The most common types of resonator filters are based on LC (inductor-capacitor) circuits or RLC (resistor-inductor-capacitor) circuits The resonant frequency of the filter can be adjusted by varying the values of the inductors, capacitors, or resistors in the circuit [4]
Consider an example of a resonator filter with a resonant frequency of 1 kHz The LC circuit consists of an inductor with an inductance of 100 mH and a capacitor with a capacitance of 10 nF By using these values, we can calculate the resonant frequency using the formula:
Resonant frequency (f) Substituting the values: f ≈ 1591 Hz
So, in this example, the resonant frequency of the filter would be approximately 1.591 kHz.
Here are some applications of resonator filter:
Hamming Window
The Hamming window is a mathematical function used in signal processing and other applications to taper the edges of a signal, most notably in Fourier analysis.
The mathematical expression for the Hamming Window is given by:
(Eq.3) where: is the value of the window at sample n
N is the total number of samples
By multiplying each sample of the signal by the corresponding sample by this formula:
- Example import numpy as np import matplotlib.pyplot as plt
L = float(input("Please enter the window length: ")) #Define window length with "float"
#used for converting the input of the user into float data type hamming = np.hamming(L) #Generating the Hamming window function by using the keyword "hamming" plt.plot(hamming) plt.title("Hamming window") plt.xlabel("Sample") plt.ylabel("Amplitude") plt.show
To generate the Hamming window in this example, we use the NumPy library The length of the window is set based on the input of the user The np.hamming function computes the Hamming window coefficients based on the length specified Finally, we use Matplotlib to visualize the window's shape.
The resulting plot will depict a symmetric window with a main lobe in the center and smaller sidelobes on either side.
Some of the applications of Hamming Window
Problem 1
Code: a = [1,0.7,0.6]; b = [1,-1.5,0.9]; x = zeros(1,100); x(1) = 1; h = filter(b,a,x); h = h' stem(h) xlabel('n') ylabel('h[n]') title('Impulse response of filter') c)
Code: close all; a = [1, 0.7, 0.6]; b = [1, -1.5, 0.9]; x = zeros(1, 100); x(1) = 1; h = filter(b, a, x); h = h';
% Plot pole-zero plot figure; zplane(b, a);
% Plot frequency response figure; q = fftshift(fft(h)); subplot(2, 1, 1); plot(linspace(-pi, pi, length(q)), abs(q)); grid on; grid minor; xlim([-pi, pi]); title('Magnitude of Frequency Response'); ylabel('|H(e^{j \Omega})|'); xlabel('\Omega'); plot(linspace(-pi, pi, length(q)), 180/pi*angle(q)); grid on; grid minor; xlim([-pi, pi]); title('Angle of Frequency Response'); ylabel('\angle H(e^{j \Omega})'); xlabel('\Omega');
Problem 2
%Define the signal x = 2*sin(2*pi*f1*t*Ts) + sin(2*pi*f2*t*Ts) + 1.5*sin(2*pi*f3*t*Ts);
%Calculate the 2048-point FFT fft_2048 = fft(x,L);
%Calculate the magnitude spectrum mag_spec = abs(fft_2048)/L;
%Define freq axis f = linspace(0, Fs/2, L/2+1); figure; plot(f, mag_spec(1:L/2+1)); xlabel('Freq (Hz)'); ylabel('Magnitude spectrum (mag_spec)'); title('Magnitude spectrum of the signal with omega = 50 Hz'); xlim([0,200]); %Set limitation to 200 Hz b)
%define the min difference in the component freq delta_min_freq = min(f2-f1, f3-f2);
%Using formula to calculate the minimum number of samples
L_min = fs/delta_min_freq; disp(['Minimum number of samples to achieve sufficient freq (measured in samples): ', num2str(L_min)]);
% Plot the signal figure; subplot(2, 1, 1); plot(t, x); xlabel('Time (s)'); ylabel('Amplitude'); title('Generated Signal'); grid on;
% Plot the magnitude spectrum subplot(2, 1, 2); plot(f, mag_spec(1:L/2+1)); xlabel('Frequency (Hz)'); ylabel('Magnitude Spectrum'); title('Magnitude Spectrum of the Generated Signal'); xlim([0, 200]); % Set limitation to 200 Hz grid on; c)
%Hamming window hamming_window = 0.54-0.46*cos((2*pi*t)/(L-1)); x_hamming = x.*hamming_window;
%Calculate the FFT of the Hamming window fft_hamming = fft(x_hamming, L);
%Calculate the magnitude spectrum mag_spec_hamming = abs(fft_hamming)/L;
%Define freq axis f = linspace(0, fs/2, L/2+1);
% Plot the signal figure; subplot(3, 1, 1); plot(t, x); xlabel('Time (s)'); ylabel('Amplitude'); title('Generated Signal'); grid on;
% Plot the magnitude spectrum subplot(3, 1, 2); plot(f, mag_spec(1:L/2+1)); xlabel('Frequency (Hz)'); ylabel('Magnitude Spectrum'); title('Magnitude Spectrum of the Generated Signal'); xlim([0, 200]); % Set limitation to 200 Hz grid on;
% Plot the Hamming-windowed signal subplot(3, 1, 3); plot(t, x_hamming); xlabel('Time (s)'); ylabel('Amplitude'); title('Hamming-Windowed Signal'); grid on; figure;
% Plot the magnitude spectrum of the Hamming-windowed signal plot(f, mag_spec_hamming(1:L/2+1)); xlabel('Frequency (Hz)'); ylabel('Magnitude Spectrum'); title('Magnitude Spectrum of Hamming-Windowed Signal'); xlim([0, 200]); % Set limitation to 200 Hz grid on;
Problem 3
Code: function [G, a1, a2] = ResonatorFilter_NU(fs, wp, delta_omega)
% Get input from the user fs = input('Enter the sampling frequency (fs) in Hz: '); wp = input('Enter the peak frequency (wp) in Hz: '); delta_omega = 0.5 * wp; % Width in Hz
% Call the function to get filter coefficients
[G, a1, a2] = ResonatorFilter_NU(fs, wp, delta_omega);
% Plot the frequency response frequencies = linspace(0, 15e6, 1000); % Frequency range from 0 to 15 MHz
H = freqz([-1 -2 1 + a1 a2], G, frequencies, fs); figure; plot(frequencies / 1e6, 20 * log10(abs(H))); xlabel('Frequency (MHz)'); ylabel('Magnitude (dB)'); title('Resonator Filter Frequency Response'); grid on;
Problem 4
N = 500; fs = 500; % Sampling rate t = (0:N-1)/fs; % Time vector x = ecg(N); % Generate cardiogram signal noise = 0.1 * randn(size(x)); % Generate random noise y = x + noise; % Define function y(t)
% 4.a w_cut_off = 15; % Cut off freq n = 10; % Filter order
% Design the FIR low-pass filter h = fir1(n, w_cut_off/(fs/2), 'low');
% Filter the noisy signal filtered_y = filtfilt(h, 1, y);
% Plot the original and filtered signals figure; plot(t, y, 'b'); hold on; plot(t, filtered_y, 'r'); xlabel('Time (s)'); ylabel('Signal amplitude'); legend('Original signal', 'Filtered signal'); title('Noisy cardiogram signal and filtered signal');
Results of Problem 1
Figure 2: Impulse response of filter c)
Figure 3: The pole/zero pattern of the impulse response
Figure 4: The magnitude (in dB) and phase (in degree) spectra of the filter
Results of Problem 2
Figure 5: The magnitude spectrum of the signal with omega = 50Hz b)
Minimum number of samples to achieve sufficient frequencies (measured in samples): 3000000
Figure 6: Generated Signal and Magnitude spectrum of the Generated Signal c)
Figure 7: Hamming-Windowed Signal with Genrated Signal and Magnitude spectrum of the
Figure 8: Magnitude spectrum of Hamming-Windowed Signal
Results of Problem 3
Figure 9: Resonator Filter Frequency Response
Figure 10: Noisy cardiogram signal and filtered signal
In this lab, we have learnt the concepts of Fourier transform, transfer function, resonator filter and Hamming window and their applications in real-life We have also learnt how to use some of