Lab I12

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8
At a glance
Powered by AI
The document discusses sampling and reconstructing signals using the discrete Fourier transform (DFT). It demonstrates the effect of data window size and number of points in the FFT on the spectrum of sinusoidal signals.

The sampling period is 1/8192 seconds.

The frequency of the cosine x[n] is 10 Hz.

INC 212 Signals & Systems SW-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-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 =l i nspace( 0, 2, 50) ; %vect or of t i me var i abl e
T=t ( 2) - t ( 1) ; %sampl i ng per i od
x=cos( 2*pi *t / ( 10*T) ) ; %t he cosi ne wi t h 50 sampl es at 10 sampl es/ per i od

The plot of the sequence x[n] from n =0 to 49 is shown in Fig. 4

st em( [ 0: 49] , x) ; %pl ot of t he sequence x[ n]
0 5 10 15 20 25 30 35 40 45 50
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
Sample (n)
x
[
n
]

Fig. 4 A cosine with 50 samples at 10 samples per period

INC 212 Signals & Systems SW-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.

%Def i ne 3 di f f er ent val ues of N
N1 = 64;
N2 = 128;
N3 = 256;

%Comput e t he magni t udes of FFT of x[ n] f or each of 3 val ues of N
X1 = abs( f f t ( x, N1) ) ;
X2 = abs( f f t ( x, N2) ) ;
X3 = abs( f f t ( x, N3) ) ;

%Fr equency scal i ng t o conver t t he di scr et e f r equenci es k t o t he
cont i nuous f r equency i n Hz
f s=1/ T; %sampl i ng f r equency i n Hz
F1 = [ 0 : N1/ 2] / N1*f s;
F2 = [ 0 : N2/ 2] / N2*f s;
F3 = [ 0 : N3/ 2] / N3*f s;

%pl ot t he t r ansf or ms
f i gur e;
subpl ot ( 3, 1, 1)
st em( F1, X1( 1: N1/ 2+1) , ' f i l l ed' ) , t i t l e( ' N = 64' ) , axi s( [ 0 12. 3 0 30] )
subpl ot ( 3, 1, 2)
st em( F2, X2( 1: N2/ 2+1) , ' f i l l ed' ) , t i t l e( ' N = 128' ) , axi s( [ 0 12. 3 0 30] )
subpl ot ( 3, 1, 3)
st em( F3, X3( 1: N3/ 2+1) , ' f i l l ed' ) , t i t l e( ' N = 256' ) , axi s( [ 0 12. 3 0 30] )
xl abel ( ' Fr equency ( Hz) ' )

INC 212 Signals & Systems SW-4
0 2 4 6 8 10 12
0
10
20
30
N =64
0 2 4 6 8 10 12
0
10
20
30
N =128
0 2 4 6 8 10 12
0
10
20
30
N =256
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.

%Dat a gener at i on
t =l i nspace( 0, 2, 50) ;
T=t ( 2) - t ( 1) ;
x1=cos( 2*pi *t / ( 10*T) ) ; %5 per i ods
x2=r epmat ( x1, 1, 2) ; %10 per i ods
x3=r epmat ( x1, 1, 5) ; %25 per i ods

N = 1024; %t he number of FFT

%Comput e t he magni t udes of FFT of x[ n] f or each of 3 dat a l engt hs
X1 = abs( f f t ( x1, N) ) ;
X2 = abs( f f t ( x2, N) ) ;
X3 = abs( f f t ( x3, N) ) ;

INC 212 Signals & Systems SW-5
%Fr equency scal i ng
f s=1/ T; %sampl i ng f r equency i n Hz
F = [ 0 : N/ 2] / N*f s;

%pl ot t he t r ansf or ms
f i gur e;
subpl ot ( 3, 1, 1)
pl ot ( F, X1( 1: N/ 2+1) ) , t i t l e( ' 5 per i ods' )
subpl ot ( 3, 1, 2)
pl ot ( F, X2( 1: N/ 2+1) ) , t i t l e( ' 10 per i ods' )
subpl ot ( 3, 1, 3)
pl ot ( F, X3( 1: N/ 2+1) ) , t i t l e( ' 25 per i ods' )
xl abel ( ' Fr equency ( Hz) ' )

0 2 4 6 8 10 12 14
0
20
40
5 periods
0 2 4 6 8 10 12 14
0
50
10 periods
0 2 4 6 8 10 12 14
0
100
200
25 periods
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 =l i nspace( 0, 2, 50) ; %vect or of t i me var i abl e
T=t ( 2) - t ( 1) ; %sampl i ng per i od
x=cos( 2*pi *t / ( 10*T) ) ;
INC 212 Signals & Systems SW-6
2. Create the DFT of the signal and plot its magnitude and phase.

Xk=f f t ( x) ; %Di scr et e Four i er t r ansf or mof x
f i gur e
subpl ot ( 2, 1, 1) , st em( abs( Xk) , ' . ' ) , t i t l e( ' Magni t ude of FFT' ) ;
subpl ot ( 2, 1, 2) , st em( angl e( Xk) , ' . ' ) , t i t l e( ' Phase of FFT' ) ;



3. Recover the time-domain function by using the inverse DFT.

xr = r eal ( i f f t ( Xk) ) ;
f i gur e
subpl ot ( 2, 1, 1) , pl ot ( t , x) , t i t l e( ' Cosi ne bef or e FFT' ) ;
subpl ot ( 2, 1, 2) , pl ot ( t , xr ) , t i t l e( ' Cosi ne af t er FFT and I FFT' ) ;

INC 212 Signals & Systems SW-7
LAB ASSIGNMENT

TASK I: SAMPLING AND ALIASING
(Adapted from J .R. Buck et al, Computer Explorations in Signals and Systems using
Matlab, 2
nd
edition, Prentice Hall, New J ersey.)

Consider the sinusoidal signal ) sin( ) (
0
t t x O = . If x(t) is sampled with frequency
) 8192 ( 2t e =
s
rad/sec.
1) Assume ) 1000 ( 2
0
t = O 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 1 0 < s t . Create a vector x
which contains the samples of ) (t x at the time samples in t. The vector x represents the
discrete-time signal ) sin( ] [
0
n n x O = .
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 ) 1500 ( 2
0
t = O and ) 2000 ( 2t
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 O
0
?
6) Now repeat Parts 1) 3) for the sinusoidal frequencies ) 4000 ( 2
0
t = O , ) 5000 ( 2t and
) 6000 ( 2t 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 O
0
? If not, can you explain this
behaviour?


TASK II: 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 noise removal by
using Fourier analysis of a signal. When you load the tested file chord.mat, you will get
two data sets: x is a sound signal, composed of several frequency components to perform
a chord and y is the sound signal x corrupted by a white noise. The data were sampled
with sampling rate 16 kHz for 1 second.

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 inverse DFT. This will become your filtered signal x.
3. From the DFT, estimate the Fourier coefficients of those three frequency components.
Write down the Fourier series of the estimated noise-free signal x in the trigonometric
form.

=
+ + =
m
k
k k k
t f A A t x
1
0
) 2 cos( ) ( u t
where the phase
k
u is in degrees and m is the number of musical notes in the chord.
INC 212 Signals & Systems SW-8
4. Generate the estimated signal with the same data length as x, according to the
function obtained in 3. Plot and compare the estimated function with the filtered
signal x to justify your estimation.

According to the Frequencies for equal-tempered scale
(http://www.phy.mtu.edu/~suits/notefreqs.html), what musical notes are combined in this
chord? (If you are a musician, you may be able to identify the chord!).


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.

You might also like