You are on page 1of 22

SPECTRAL ANALYSIS

TUTORIAL SHEET 2.2

NASSIR ROSUN 1711714


UNIVERSITY OF MAURITIUS
SIGNAL PROCESSING FOR TELECOMMUNICATIONS

Page | 1
Table of Content
Table of Content....................................................................................................................................1

Table of Figures.....................................................................................................................................3

Introduction...........................................................................................................................................4

Aim of Experiment............................................................................................................................4

Objectives of the experiments............................................................................................................4

Theory...................................................................................................................................................5

DFT Matlab.......................................................................................................................................5

Windowing........................................................................................................................................5

Methodology.........................................................................................................................................6

Experiment 1.....................................................................................................................................6

MATLAB Implementation............................................................................................................6

Experiment 2.....................................................................................................................................7

MATLAB Implementation............................................................................................................7

Experiment 3.....................................................................................................................................8

MATLAB Implementation............................................................................................................8

Results...................................................................................................................................................9

Experiment 1.....................................................................................................................................9

Experiment 2...................................................................................................................................10

Experiment 3...................................................................................................................................11

Discussion...........................................................................................................................................12

Experiment 1...................................................................................................................................12

Experiment 2...................................................................................................................................12

Experiment 3...................................................................................................................................14

Conclusion...........................................................................................................................................14

References...........................................................................................................................................15

Appendix A.........................................................................................................................................16

Appendix B.........................................................................................................................................17

Page | 2
Appendix C.........................................................................................................................................18

Student Name : Mohammad Nassir-Ud-Diin Ibn Nazir Rosun


Student ID : 171714
Programme : BEng (Hons) Telecommunications Engineering with Networking

Module Code : ELEC 3069Y (5)


Module Name : Signal Processing for Telecommunications

Experiment Title : Practical Sheet #2.2 Spectral Analysis


Date Performed : 14th February 2020
Lab Partner’s Name : ……………………………………….

UNDERTAKING
I hereby claim that all part of this report is my own work, and that no part has been copied except
where reference has been made to appropriate source(s). I understand that plagiarism constitutes a
serious offence that may lead to action taken at the Disciplinary Committee.

Student Signature : .........................................................................


Student Name : Mohammad Nassir-Ud-Diin Ibn Nazir Rosun
Date : .........................................................................

Page | 3
Table of Figures
Figure 1 A periodic waveform and its frequency spectrum...................................................................4
Figure 2 Subplot Showing Original signal, DFT of signal.....................................................................9
Figure 3 Reconstructed Signal X[n]......................................................................................................9
Figure 4 Amplitude spectrum of Complex sinusoid............................................................................10
Figure 5 DFT Spectrum showing frequency resolution.......................................................................10
Figure 6 Sampled data and Frequency Spectrum.................................................................................11
Figure 7 Windowed Signal Y[n] in time domain.................................................................................13

Page | 4
Spectral analysis
Introduction
Spectrum analysis, also referred to as frequency domain analysis or spectral density estimation, is the
technical process of decomposing a complex signal to show how the signal's energy is distributed
over a range of frequencies. The frequency-domain representation of a signal carries information
about the signal's magnitude and phase at each frequency. A signal can be converted between the time
and frequency domains with a pair of mathematical operators called a transform. An example is the
Fourier transform, which decomposes a function into the sum of a (potentially infinite) number of sine
wave frequency components. In practice, nearly all software and electronic devices that generate
frequency spectra utilize a discrete Fourier transform (DFT), which operates on samples of the signal,
and which provides a mathematical approximation to the full integral solution. The DFT is almost
invariably implemented by an efficient algorithm called fast Fourier transform (FFT).

Figure 1 A periodic waveform and its frequency spectrum

Aim of Experiment
The aim of the experiment was to observe the frequency spectrum to analyse the spectral content of
different types of signals.

Objectives of the experiments


The objectives of the experiments are as follows:

 Calculate the N-point DFT of energy signals and recover the signal by using the inverse-DFT.
 Investigate the effect of different type of windows used to truncate signals for performing
DFT.
 Reconstruct a continuous-time signal by taking the DFT on sampled signal and analysing its
frequency spectrum.

Page | 5
Theory (From Lecture Notes)
Consider an energy whose X [n ] whose DTFT, denoted as X ( jw) is finite and continuously defined
on the interval −π <ω< π . The Discrete Fourier Transform (DFT) is the equivalent of the continuous
Fourier Transform sampled at regularly spaced frequency points ω k ,where ω k is equal to

k∗2 π
ωk= where K =0,1,2,3 … . N −1 …(1.0)
N

From definition, the DFTF of a signal X [ n ] can be expressed as:

+∞
X ( jw ) =∑ X [n]e (− j ω n) …(1.1)
−∞

Now, we let X ( k ) denoted the sampled values of X ( jw) , where k is the frequency index i.e.

X(k) = X ( j ωk ). Therefore, the DFT can be expressed as:

m−1 −jk n2π


( )
N
X (k )= ∑ X [n]e …(1.2)
n=0

~
And applying the inverse DFT (IDFT) on X (k ) gives us the reconstructed signal X [n].

N −1 jk n2π
~ ( N )
X [n]= ∑ X [ k ] e …(1.3)
k=0

DFT Matlab
Practical implementations of the DFT are usually based on one of the Cooley-Tukey “`Fast Fourier
Transform” (FFT) algorithms [1]. For this reason, the MATLAB DFT function is called ‘fft’, and the
actual algorithm used depends primarily on the transform length N. The fastest FFT algorithms
generally occur when N is a power of 2.

Windowing
The FFT transform assumes that it is using a finite data set, a continuous spectrum that is one period
of a periodic signal. However, any disparity between the last sample in the set and the repeated first
sample will lead to anomalies being introduced in the frequency spectrum, usually higher than the
Nyquist frequency that are aliased between 0 and half of your sampling rate. This phenomenon is
known as spectral leakage.

Windowing is the process of taking a small subset of a larger dataset, for processing and analysis.
Different types of windows exist for different purpose. They work by reducing the amplitude of the

Page | 6
discontinuities at the boundaries of each finite sequence acquired by the digitizer. the resulting
frequency components are lower in magnitude; thus, the amount of error is reduced.

Page | 7
Methodology
Experiment 1
The first part of the experiment was to investigate an energy signal in the time and frequency domain.
We calculated the N-point DFT of the signal using equation 1.2 and the reconstructed the signal from
the DFT using equation 1.3.

MATLAB Implementation
(See Appendix A for codes)

This section describes the steps needed to plot the signal in time domain, calculate the N-point DFT
and reconstruct the signal from the DFT spectrum.

1. To plot the signal in the time domain, we generate a vector of 100 evenly spaced points in the
interval 0<t <n where n has been chosen to be 12. We then multiply the equation by t and
plot the result using the plot(x,y) function.

2. To calculate the N-point DFT using equation 1.2, we use two for loops:
 1st loop to iterate through all the values of K where −11< k <12.
− jkn 2 π
 2nd loop to calculate the summation of X [n ]e( N
)
where 0 ¿ n<t −1.

3. The magnitude spectrum was obtained by using the abs(x) method on the DFT vector which
was then plotted using the stem(x,y) to observe the impulse at the different frequencies.

4. To reconstruct the signal, we use the equation 1.3 which is defined as the IDFT. To compute
the IDFT, we use two for loops:
 1st loop to iterate through all the values of n where 0¿ n<3∗¿).
jkn 2 π
 2nd loop to calculate the summation of X [k ]e( where −11< k <12.
)
N

5. The magnitude of IDFT was obtained by using the abs(x) method and then, was plotted using
the plot(x,y).

Page | 8
Experiment 2
The seconds part of the experiment was to investigate the effect of windowing on DFT. Window is a
technique used to truncate a longer sequence of data into a small and finite length.

MATLAB Implementation
(See Appendix B for codes)

This section describes the steps needed to computed and plot the amplitude spectrum of a signal.

1. Declare signal y (n) which is equal to x (n)1+ x( n)2 where,

 x ( n )1=5 cos ( π5 n)∧¿ x ( n ) =5 cos ( π2 n)¿ where 0< n<100


2

2. Create a rectangular window wr of size 100 using the ones() function in matlab.
3. Obtain the truncated signal by multiplying y (n) and wr .
4. We then declare a variable of ω c that hold a range of normalized frequency from -π rad to π
rad.
5. We calculate the DFT of y (n) using the fft () function using 1000 points so that M > N.
6. The fftshift() function is used to shift the zero-frequency components to the centre to obtain a
frequency spectrum centred at zero.
7. The amplitude spectrum is obtained by plotting the normalized frequency versus the absolute
value of the DTFT, which is obtained by using the abs(x) function.
8. The separation between w 1 and w 2 was decreased until the two components were just
distinguishable in the spectrum.
9. The window type was then changed from rectangular to hamming by using the inbuilt
hamming() function of matlab.
10. The amplitude spectrum for step 9 was plotted in the linear scale so that the peaks would be
clearly.
11. Then, steps 1 to 8 was repeated and the amplitude spectrum of the signal was then plotted.
12. The windowed signal was then plotted in the time domain and its enveloped was also shown n
the plot to show the effect of the window, if any.

Page | 9
Experiment 3
The third part of the experiment was to analyse the amplitude spectrum of a sampled signal and then
determine the frequencies (in Hz) and amplitudes of all spectral components present in the
continuous-time signal.

MATLAB Implementation
(See Appendix C for codes)

This section describes the steps needed to read the samples from a text file, evaluate its amplitude
spectrum and determining the spectral components.

1. The readings from the txt file is first loaded in memory by assign it a file id using the fopen()
function in matlab.
2. The fscan() function is then used to read the reading line by line, this function takes as input 2
arguments:
 fileID – the file identifier of an open text file, specified as an integer.
 formatSpec – format of the data fields in the file, specified as a character vector.
Since we are reading data formatted as doubles, the formatSpec is %f which is
floating-point fields.
3. The first 100 samples of the sampled data were then plotted to observe the signal element.
4. The DFT of the samples were then calculated by using the fft() function and the fftshift()
function was used to shift the zero-frequency component in the middle of the spectrum.
5. The fft result is also divided by the signal length to normalize to normalize the amplitude to
A.
6. The magnitude spectrum was obtained by taking the absolute value of the DFT spectrum by
using the abs() function.
7. The magnitude is converted to the decibel scale using the mag2db() function which uses the
conversion formula 20 log 10 (x) .
8. To observe the spectral components of the sampled signal, we plot the positive part of the
spectrum and we observe the peaks found on the spectrum.

Page | 10
Results
Experiment 1
The result of experiment 1 are shown in the following section.

The signal X[n] is an exponential decay energy signal of about 10 samples, if we ignore values below
0.01. The DTF of the signal is shown in the second figure.

Figure 2 Subplot Showing Original signal, DFT of signal

The following figure shows the reconstructed signal plotted for 3 time period of the original signal.
From the figure, we can observe that X̌ [n] contains non-overlapping time shifted image of X [n ]. We
can extract X [n ] from X̌ [n] by taking a block from n = 0 up to N-1, giving us an exact recovery of
X[n].

Page | 11
Figure 3 Reconstructed Signal X[n]

Experiment 2
The result of experiment 2 are shown in the following section.

Figure 4.0 below shows the subplot of the DFT obtained when we applied a rectangular window and a
hamming window on a complex sinusoid. The DFT has been plotted in the log scale to have a better
view of the side lobes. The magnitude of the main lobe, first side lobe and last side lobe has also been
shown in the graph for analysis.

Figure 4 Amplitude spectrum of Complex sinusoid

Page | 12
The following figure shows the DFT spectrum obtained when the separation between the 2

1
frequencies was decreased up to π rad .
50

Figure 5 DFT Spectrum showing frequency resolution

Experiment 3
The result of experiment 3 are shown in the following section.

The subplot below shows the first 100 samples of the sampled signal and the frequency spectrum of
the of the signal with the peaks showing the frequencies present in the signal.

Page | 13
Figure 6 Sampled data and Frequency Spectrum

Page | 14
Discussion
Experiment 1
As seen from figure 3, the DFT of the energy signal has a DC component of amplitude 4.5 follows by
12 harmonics of ω=0.2618 rad from -π rad to π rad. These peaks represent the frequencies that are
present in the energy signal and the amplitude of these frequencies can be read from the graph.
However, these frequencies have been sampled from the DTFT of the signal i.e. not all the
frequencies that were present in the DTFT are present.

To be able to reconstruct our signal from the DFT, we need a sampling rate in the frequency domain
that will prevent aliasing in the time domain. Therefore, we need to choose a value for N that is
greater than the signal’s duration, M. For this experiment, the value of N was chosen as 24 as the
signal duration was about 10 samples ignoring values less than 0.01. In this case, the condition N > M
is satisfied meaning that we can reconstruct the signal by taking the inverse-DFT.

Experiment 2
In experiment 2, we have a complex sinusoid Y [n] that is made up of two cos waves, each with
different frequencies. When we applied a rectangular window of length 100, to Y [n], the first plot in
figure 3 was obtained. The spectrum consists of 2 of peaks, one at 0.25π rad and another at 0.50π rad,
corresponding to the 2 frequencies that was used to generate Y [n]. We also have multiples sides
lobes, a result of the abruptness of the window's transition from 1 to 0 in the time domain. This
abruptness of the window discontinuity in the time domain is what determines the side-lobe roll-off
rate.

When the separation between ω 1 and ω 2 also known as ∆ f was decreased, the peaks came closer
until they merged into a single peak. The separation at which the peaks merged was not the same for
each window. The peaks of the hamming window were already indistinguishable for a ∆ f of greater

1
than π while the rectangular window had 4 peaks clearly visible. This confirms that the
50
rectangular has a better frequency resolution than the hamming window.

Features Rectangular Window Hamming Window


Main Lobe Amplitude (dB) 7.5286 2.5579
1st Side Lobe Amplitude (dB) -5.1085 -40.9169
1st Side Lobe relative level (dB) -12.6 -43.4
When we changed window to a hamming window, the second plot in figure 3 was obtained. As we
can observe from the graph, the side lobes are much smaller than the rectangular window. The main
lobe and side lobes amplitude together with the calculated first side lobe level are summarized in the
table 1.0 below.

Page | 15
Table 1 First Side Lobe Level

From the second plot in figure 3, we can observe that the main lobe amplitude for the hamming
window has decrease as compared to that of the rectangular window. This decrease in amplitude can
be observe in the time domain plot of the filtered signal as shown in figure 5 below.

Figure 7 Windowed Signal Y[n] in time domain

The envelope decreases the magnitude of the discontinuities between the last sample and the first
sample resulting in lower side lobes in the frequency spectrum. However, the window also distorts the
data in two different ways:

 Amplitude – The amplitude of the signal is reduced.


 Energy – The area under the curve (energy of the signal) is reduced.

To correct for this distortion, window correction factors are used to try and compensate for the effects
of applying a window to data [3] [4]. For this experiment, the correction factor was calculated to be
1.867 for the hamming window. So, the correct amplitude is Ac = A graph∗C factor. This can be verified
by using the value obtained in figure 5, the main lobe amplitude is 1.34 so the corrected amplitude is
calculated to be 1.867 * 1.34 which is equal to 2.501 as expected.

Page | 16
Experiment 3
In this experiment, we have a signal that was sampled at 1000Hz. According to Nyquist Theorem [4],
the maximum frequency that can be present in the signal is half the sampling frequency that is 500Hz.
Examining the samples, we observe that it is amplitude limited between -9.9125 and 10.02. This is
obtained using the min and max function in matlab.

>> max(samples)
ans =10.0200
>> min(samples)
ans = -9.9125

To observe its frequency spectrum, we plotted its DFT spectrum in the log scale and obtained the
second plot in figure 4. Using the log scale makes it easier to view wide dynamic ranges; that is, it is
easy to see small signal components in the presence of large ones. We observed 2 peaks, one at 256Hz
of amplitude 18.06dB and a second peak at 51 Hz of amplitude 6.02dB. The spectral components
present in the sampled signal is summarized in the table below.

Frequency (Hz) Amplitude (dB) Amplitude


50 6.0208 2
245 18.0618 8

Table 2 Frequencies present in sampled signal

Conclusion
The aim of the experiment was to analyse the spectral content of a signal by calculating its’ DFT
spectrum using specialised software. We found that by using the FFT algorithm over a sequence of
length N, we can evaluate the amplitude spectrum to examine the different frequencies present. To
reconstruct the signal, we used the IDTF algorithm which is implement as ifft in software, it was
observed that by using N greater than the signal duration M, the signal can be reconstructed from its
DFT spectrum. We also investigate the effects of windowing where it was found that the Hamming
window resulted in lower side lobes level than a rectangular window at the cost of some variation in
the windowed samples which can be corrected by using a window correction factor. However, the
frequency resolution of a rectangular window was found to be better than the hamming window as for
closely spaced frequencies, the rectangular window was able to isolate them whereas the peaks in the
hamming window has combined together.

Page | 17
References
[1] J. Cooley and J. Tukey, ``An algorithm for the machine computation of the complex Fourier
series,'' Mathematics of Computation, vol. 19, pp. 297-301, Apr. 1965.

[2] "FFT example on MATLAB help - MATLAB Answers - MATLAB Central", Mathworks.com,


2020. [Online]. Available: https://www.mathworks.com/matlabcentral/answers/45170-fft-example-
on-matlab-help. [Accessed: 21- Feb- 2020]

[3] Brandt, A., 2011. Noise and vibration analysis: signal analysis and experimental procedures. John
Wiley & Sons. [Accessed: 21- Feb- 2020]

[4] "Calculate windowing correction factor - MATLAB Answers - MATLAB


Central", Mathworks.com, 2020. [Online]. Available:
https://www.mathworks.com/matlabcentral/answers/372516-calculate-windowing-correction-factor.
[Accessed: 22- Feb- 2020]

Page | 18
Appendix A
Code for experiment 1

clc;
clearvars;
N = 24;
t = linspace(0,N,N);
sig = exp (-(t/5).^2);
dft_s = zeros([1,N]);
idft_d = zeros([1,N*3]);
k = -11 : 12;

for k = k
for n = 0 : numel(t) - 1
dft_s(k + 12) = dft_s(k + 12) + sig(n + 1)* exp((-1j*k*2*pi*n)/N);
end
end

for n = 0 : 3*(N-1)
for k = -11 : 12
idft_d(n + 1) = idft_d(n + 1) + (dft_s(k +
12)*exp(1j*2*pi*k*n/N));
end
end

k = -11:12;
idft_d = idft_d./N;
Wk = (2*pi*k)/N;

figure(1);
subplot(121);
stem(t,sig);hold on;
plot(t,sig,'b--o');
ylabel("Magnitude");
xlabel("n");
title("Original Signal x[n]");

subplot(122);
stem(Wk,abs(dft_s));
xlabel("W(K)");
ylabel("Magnitude");
title("DFT x[k]");

t = 0:(3*N)-1;
figure;
plot(t,abs(idft_d));
ylabel("Magnitude");
xlabel("n");
title("Reconstructed Signal x[n]");

Page | 19
Appendix B
Code for experiment 2

clc;
clearvars;

N = 100;
M = 1000;
n = 0:N-1;
Yn = 5 * cos (0.25*pi*n) + 5 * cos ((0.5)*pi*n);

Wr_1= ones(1,N);
Yf_1= Wr_1 .* Yn;
W = linspace(-pi,pi,M)/pi;
X = fftshift(fft(Yf_1,M)/N);

figure (1);
subplot(211);
plot(W,mag2db(abs(X)));
grid on;
grid minor;
xlabel('Normalized Frequency (\times\pi rad)');
ylabel('|x(jw)|');
title("Amplitude Spectrum with Rectangular Window");

M = 100;
nfft= 1000;
n = 0 : M-1;

ham = hamming(M);
Yf_2 = Yn .* ham';
W = linspace(-pi,pi,nfft)/pi;
X2 = fftshift(fft(Yf_2,nfft)/N);

subplot(212);
plot(W, (abs(X2)));
grid on;
grid minor;
xlabel('Normalized Frequency (\times\pi rad)');
ylabel('|x(jw)|');
title("Amplitude Spectrum with Hamming Window");

figure;
subplot(211);
envelope(Yf_2,8,'peak');
title("Peak Envelope for hamming window")
subplot(212);
envelope(Yf_1,8,'peak');
ylim([min(Yf_1)-2 , max(Yf_1)+2]);
title("Peak Envelope for rectangular window")

Page | 20
Aw = length(ham)/sum(ham);

Appendix C
Code for experiment 3

clc;
clearvars;

fileID = fopen('samples.txt','r');
samples = fscanf(fileID,'%f');
L = numel(samples);
Fs = 1000;
l = 50;

x_fft = fftshift(fft(samples))/L;
mag = (abs(x_fft));
dB = mag2db(2*mag(numel(mag)/2:end));
f = linspace(0,500,numel(dB));

figure (1);
subplot(211);
stem(samples(1:100));
grid on;
grid minor;
ylabel("Magnitude |X[n]|");
xlabel("t [n]")
title("Samples");

subplot(212);
plot(f,(dB));
grid on;
grid minor;
ylabel("Magnitude (dB)");
xlabel("Frequency (Hz)")
title("DFT of Samples");

Page | 21

You might also like