Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
signal homework
1. LABORATORY - FREQUENCY ANALYSIS OF DISCRETE-TIME SIGNALS
INTRODUCTION
The objective of this lab is to explore many issues involved in sampling and
reconstructing signals, including analysis of the frequency content of sinusoidal signals
using the DFT.
When the sequence x[n] is of finite duration, the frequency contents that are
contained in the sequence can be determined using the Discrete Fourier Transform (DFT).
The DFT is an approximation of the Discrete Time Fourier Transform (DTFT), which is
computed over an infinite duration. Computation of the DFT using only N data points may
exhibit the edge effect when the windowed periodic signal is nothing like the original
signal as shown in Fig. 1.
Windowed data
(a)
(b)
Fig. 1 Edge effect of the windowed data. (a) original sinusoidal sequence. (b) windowed
periodic signal.
Extracting a finite length of data can be thought of as multiplying the sequence with a
rectangular box of length N which has a sinc-like spectrum as shown in Fig. 2. The sinc-
like spectrum of the rectangular window makes the resulting spectrum of the windowed
sinusoid in Fig. 1 appear like a sinc-function, instead of an impulse as expected.
(a) (b)
Fig. 2 A rectangular window (a) Time domain (b) Frequency domain
INC 212 Signals & Systems SW-1
2. Fig. 3 Spectrum of windowed sinusoid
EXERCISE 1: EFFECT OF THE NUMBER OF POINTS IN THE FFT
In this exercise, we will demonstrate the effect of data window and changing the
value of the number of points in the FFT, N, on the FFT of sinusoidal signals.
1. Let us start by synthesising a cosine with 50 samples at 10 samples per period.
t=linspace(0,2,50); % vector of time variable
T=t(2)-t(1); % sampling period
x=cos(2*pi*t/(10*T)); % the cosine with 50 samples at 10 samples/period
The plot of the sequence x[n] from n = 0 to 49 is shown in Fig. 4
stem([0:49],x); % plot of the sequence x[n]
1
0.8
0.6
0.4
0.2
x[n]
0
-0.2
-0.4
-0.6
-0.8
-1
0 5 10 15 20 25 30 35 40 45 50
Sample (n)
Fig. 4 A cosine with 50 samples at 10 samples per period
INC 212 Signals & Systems SW-2
3. Q1. What is the sampling period of x[n]?
Q2. What is the frequency of the cosine x[n] in Hz?
Q3. What kind of spectrum do you expect from this signal?
2. The frequency contents of the sequence will be determined using the MATLAB
command FFT. The syntax for computing the FFT of a signal is FFT(x,N) where x is the
discrete signal x[n] you wish to transform and N is the number of points in the FFT. If N is
omitted, the FFT will generates the N-point FFT where N is the length of x.
Define 3 different values of N, i.e. N=64, 128, and 256. Compute a set of N-
point FFT X(k) and plot the results from k=0-N/2.
% Define 3 different values of N
N1 = 64;
N2 = 128;
N3 = 256;
% Compute the magnitudes of FFT of x[n] for each of 3 values of N
X1 = abs(fft(x,N1));
X2 = abs(fft(x,N2));
X3 = abs(fft(x,N3));
% Frequency scaling to convert the discrete frequencies k to the
continuous frequency in Hz
fs=1/T; % sampling frequency in Hz
F1 = [0 : N1/2]/N1*fs;
F2 = [0 : N2/2]/N2*fs;
F3 = [0 : N3/2]/N3*fs;
% plot the transforms
figure;
subplot(3,1,1)
stem(F1,X1(1:N1/2+1),'filled'),title('N = 64'),axis([0 12.3 0 30])
subplot(3,1,2)
stem(F2,X2(1:N2/2+1),'filled'),title('N = 128'),axis([0 12.3 0 30])
subplot(3,1,3)
stem(F3,X3(1:N3/2+1),'filled'),title('N = 256'),axis([0 12.3 0 30])
xlabel('Frequency (Hz)')
INC 212 Signals & Systems SW-3
4. N = 64
30
20
10
0
0 2 4 6 8 10 12
N = 128
30
20
10
0
0 2 4 6 8 10 12
N = 256
30
20
10
0
0 2 4 6 8 10 12
Frequency (Hz)
Fig. 5 FFT of a cosine for N=60, 128 and 256
Fig. 5 shows that the transforms all have a sinc-like appearance, which is a result of
data window, differing only in the number of samples used to approximate the sinc
function.
Q4. Record the frequencies at the peaks of all three spectra in Fig. 5. Compare these peak
frequencies with the frequency of the cosine x[n] and comment on the results.
EXERCISE 2: EFFECT OF DATA LENGTH
In this part, we will consider impact of window data length on resolution of FFT as
the FFT length kept fixed. We choose to fix N=1024 points and vary the number of
repetitions of the repetitions of the fundamental period.
% Data generation
t=linspace(0,2,50);
T=t(2)-t(1);
x1=cos(2*pi*t/(10*T)); % 5 periods
x2=repmat(x1,1,2); % 10 periods
x3=repmat(x1,1,5); % 25 periods
N = 1024; % the number of FFT
% Compute the magnitudes of FFT of x[n] for each of 3 data lengths
X1 = abs(fft(x1,N));
X2 = abs(fft(x2,N));
X3 = abs(fft(x3,N));
INC 212 Signals & Systems SW-4
5. % Frequency scaling
fs=1/T; % sampling frequency in Hz
F = [0 : N/2]/N*fs;
% plot the transforms
figure;
subplot(3,1,1)
plot(F,X1(1:N/2+1)),title('5 periods')
subplot(3,1,2)
plot(F,X2(1:N/2+1)),title('10 periods')
subplot(3,1,3)
plot(F,X3(1:N/2+1)),title('25 periods')
xlabel('Frequency (Hz)')
5 periods
40
20
0
0 2 4 6 8 10 12 14
10 periods
50
0
0 2 4 6 8 10 12 14
25 periods
200
100
0
0 2 4 6 8 10 12 14
Frequency (Hz)
Fig. 6 FFT of a cosine of 5, 10 and 25 periods
Q5. As x[n] is extended to a large number of periods, how does this affect the obtained
spectrum? Find the peak frequencies of spectra obtained from Fig. 6 and compare them
with the ones you get from Fig. 5.
EXERCISE 3: RECOVERING THE TIME-DOMAIN FUNCTION USING IFFT
Given the FFT of a signal, you can recover the time-domain function of the signal by using
IFFT. Here is how to do it.
1. Generate the cosine function as in EXERCISE 1:
t=linspace(0,2,50); % vector of time variable
T=t(2)-t(1); % sampling period
x=cos(2*pi*t/(10*T));
INC 212 Signals & Systems SW-5
6. 2. Create the DFT of the signal and plot its magnitude and phase.
Xk=fft(x); % Discrete Fourier transform of x
figure
subplot(2,1,1),stem(abs(Xk),'.'), title('Magnitude of FFT');
subplot(2,1,2),stem(angle(Xk),'.'),title('Phase of FFT');
3. Recover the time-domain function by using the inverse DFT.
xr = real(ifft(Xk));
figure
subplot(2,1,1), plot(t,x), title('Cosine before FFT');
subplot(2,1,2), plot(t,xr),title('Cosine after FFT and IFFT');
INC 212 Signals & Systems SW-6
7. LAB ASSIGNMENT
TASK I: SAMPLING AND ALIASING
(Adapted from J.R. Buck et al, Computer Explorations in Signals and Systems using
Matlab, 2nd edition, Prentice Hall, New Jersey.)
Consider the sinusoidal signal x(t ) = sin(Ω 0 t ) . If x(t) is sampled with frequency
ω s = 2π (8192) rad/sec.
1) Assume Ω 0 = 2π (1000) rad/sec and define T=1/8192, create the vector n=[0:8191], so
that t=n*T contains the 8192 time samples of the interval 0 ≤ t < 1 . Create a vector x
which contains the samples of x(t ) at the time samples in t. The vector x represents the
discrete-time signal x[n] = sin(Ω 0 n) .
2) Use subplot to display the first fifty samples of x[n] versus n using stem and the first
fifty samples of x(t) versus the sampling time using plot.
3) Calculate the Fourier transform of x(t) using the sampled data x[n]. Plot the magnitude
of the spectrum versus the frequency vector in Hz.
4) Repeat Parts 1) – 3) for the sinusoidal frequencies Ω 0 = 2π (1500) and 2π (2000)
rad/sec. Is the magnitude of the spectrum of x nonzero for the expected frequencies?
5) Play each of the sampled signals created in Part 4) using soundsc(x,1/T). Does the
pitch of the tone that you hear increase with increasing frequency Ω0?
6) Now repeat Parts 1) – 3) for the sinusoidal frequencies Ω 0 = 2π (4000) , 2π (5000) and
2π (6000) rad/sec. Also play each sample signal using soundsc. Does the pitch of the tone
that you hear increase with each increase in the frequency Ω0? If not, can you explain this
behaviour?
TASK II: DIAL TONE
2
1
0
-1
-2
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 2.2
Sample 4
x 10
Fig. 7 Dial tone signal
In this task you will analyse a dial tone signal in Fig. 7. A dial tone was produced
each time the user pressed a telephone key. It consists of 0.5 sec of tone followed by 0.1
sec of silence, which is represented by a sequence of zeros. The dial signal in Fig. 7 was
recorded at 5 kHz sampling rate. Our objective is to determine which telephone buttons
were pressed.
INC 212 Signals & Systems SW-7
8. 1. You are to write a programme which analyses which telephone keys were
pressed by the use of the FFT analysis. The touch-tone telephone tones are shown in Table
1.
Table 1. Touch-tone telephone tones
Frequencies 1209 Hz 1336 Hz 1447 Hz
697 Hz 1 2 3
770 Hz 4 5 6
852 Hz 7 8 9
941 Hz - 0 -
The dial tone file ‘dial.mat’ can be downloaded from the course website. The data
can be loaded into MATLAB by typing load dial as long as the file is in your MATLAB
PATH. The file contains the touch-tone signal, dial, which contains the sequence of touch-
tones representing a phone number of 9 digits.
You may want to extract each 0.5-sec touch tone for each digit from the touch-tone
signal and analyse its spectrum by turn. The analysing process need not be automatic. You
need to detect the peak frequencies and convert them to continuous-time frequencies in Hz,
then use Table 1 to analyse the tone. Full explanation of how you get the seven pressed
keys must be provided. Include plots to confirm your results.
2. Extra credit: You are to write a function ttdecode(x) to decode phone
numbers automatically from the touch-tones. The function accepts as its input a touch-tone
signal and returns as its output a vector containing the number. For example, if the vector
phone contained the touch-tones for the number 02-470-9096, you should get the following
output:
>> digits=ttdecode(phone);
>> digits
digits =
0 2 4 7 0 9 0 9 6
The first line of the m-file you write to implement the function should read
function digits = ttdecode(x);
Test your function by using dial as input and verify that it returns the same phone
number as you obtained in Part 1.
NB:
i) You will get full mark from the extra credit if your programme can work
with touch-tone signals where the digits and silences can have varying
lengths. It is assumed that the recorded signals are noise-free. Provide an
example to prove that your programme does work as expected.
ii) Seventy percent of full mark will be given if you programme can work
with a touch-tone signal in the format used in Part 1, i.e. 0.5 sec of touch-
tone for each digit, separated by 0.1 sec of silence.
In your report, you must specify what your programme can do, including necessary
information that the user should know, e.g. the usable format of touch-tone signals, in order
to use your programme.
INC 212 Signals & Systems SW-8
9. TASK III: Signal Extraction
A common use of Fourier transforms is to find the frequency components of a
signal buried in a noisy time domain signal. In this task you will explore two approaches of
noise removal by the use of the Fourier analysis of a signal. When you load the tested file
‘data.mat’, you will get two data sets: ‘x’ is a sound signal, composed of three frequency
components and ‘y’ is the sound signal ‘x’ corrupted by a white noise. The data were
sampled with sampling rate 10 kHz for 1 second.
Signal extraction: Method I: Fourier series-based
1. Create and observe the DFT of the noisy data ‘y’.
2. From the DFT, estimate the Fourier coefficients of those three frequency components.
ˆ
Write down the Fourier series of the estimated noise-free signal x1 in the trigonometric
form.
3. Generate the estimated signal with the same data length as ‘x’, according to the function
obtained in 2, using the same sampling frequency.
4. Create a figure with subplots: the noise-free signal superimposed with the estimated
ˆ
signal and the estimated error of method 1: e = x − x1 .
Signal extraction: Method II: FFT-based low-pass filtering
1. Create and observe the DFT of the noisy data ‘y’.
2. From the DFT, observe the significant frequency components. Eliminate the noise
by setting the DFT of all non-significant frequency components to zeros, and then perform
ˆ
the IFFT. This will become your filtered signal x 2 .
3. Create a figure with subplots: the noisy signal superimposed with the filtered signal
ˆ
and the estimated error of method 2: e = x − x 2 .
Compare and comment on the performance of the two approaches.
REPORT
- You are encouraged to discuss the lab with each other, but your solutions must
be independently written using you own words and formulations.
- Your report must be concise, but complete. Include comments, i.e. discussions
of how your results compare to your expectations, and conclusions to all of the
assignments.
- Include any plots or images you need to justify your conclusions.
- Include a representative collection of the code(s) that you used as the appendix.
- Make sure to include comments in your code explaining what it does.
- Provide a CD with the m-file ttdecode.m if you decide to do the Extra Credit
task.
INC 212 Signals & Systems SW-9