Signal Analysis using Matlab

advertisement
Signal Analysis using Matlab
1
Introduction
¾ This tutorial will demonstrate the use of
Matlab to perform the following operations:
¾ Fourier Transform
¾Autocorrelation and cross correlation
2
Fourier Transform
• The
Fourier
transform
defines
a
relationship between a signal in the time
domain and its representation in the
frequency domain.
•The time domain is displayed as a
WAVEFORM of voltage versus time,
whereas the frequency domain is shown as
a SPECTRUM of magnitude or power
versus frequency.
3
What is the DFT?
z
The Discrete Fourier Transform DFT
version of the Fourier Transform
is discrete
z
DFT is extremely important in the area of frequency
(spectrum) analysis.
z
it takes a discrete signal in the time domain and
transforms that signal into its discrete frequency domain
representation.
z
Without a discrete-time to discrete-frequency transform
we would not be able to compute the Fourier transform
with a microprocessor or DSP based system.
4
What is the FFT
z
A fast Fourier transform (FFT) is an efficient
algorithm to compute the discrete Fourier
Transform (DFT) and its inverse.
z
FFTs are of great importance to a wide variety of
applications, from digital signal processing to
solving partial differential equation to algorithms for
quickly multiplying large integers.
5
Matlab and the FFT
z
Matlab's FFT function is an effective tool for
computing the discrete Fourier transform of
a signal.
z
The following code examples will help you to
understand the details of using the FFT
function.
6
Example 1
z
The typical syntax for computing the FFT of
a signal is FFT(x,N)
z Where
z
z
x is the signal, x[n], you wish to transform
N is the number of points in the FFT.
N must be at least as large as the number
of samples in x[n].
7
Example 1 (Continue)
z
To demonstrate the effect of changing the value of N, sythesize
a cosine with 30 samples at 10 samples per period.
n = [0:29];
x = cos(2*pi*n/10);
z
Define 3 different values for N. Then take the transform of x[n]
for each of the 3 values that were defined.
N1 = 64;
N2 = 128;
N3 = 256;
X1 = abs(fft(x,N1));
X2 = abs(fft(x,N2));
X3 = abs(fft(x,N3));
z
The abs function finds the magnitude of the transform, as we are
not concerned with distinguishing between real and imaginary
components.
8
Example 1 (Continue)
z
The frequency scale begins at 0 and extends to N-1 for an
N-point FFT. We then normalize the scale so that it extends
from 0 to 1 – 1/N
z
z
z
z
F1 = [0 : N1 - 1]/N1;
F2 = [0 : N2 - 1]/N2;
F3 = [0 : N3 - 1]/N3;
Plot each of the transforms one above the other.
z
z
z
z
z
z
subplot(3,1,1)
plot(F1,X1,'-x'),title('N = 64'),axis([0 1 0 20])
subplot(3,1,2)
plot(F2,X2,'-x'),title('N = 128'),axis([0 1 0 20])
subplot(3,1,3)
plot(F3,X3,'-x'),title('N = 256'),axis([0 1 0 20])
9
Example 1 (Continue)
N = 64
20
10
0
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
0.6
0.7
0.8
0.9
1
0.6
0.7
0.8
0.9
1
N = 128
20
10
0
0
0.1
0.2
0.3
0.4
0.5
N = 256
20
10
0
0
0.1
0.2
0.3
0.4
0.5
10
Example 1 (Continue)
•Upon examining the pervious
figure, one can see that each
of the transforms adheres to
the same shape, differing only
in the number of samples
used to approximate that
shape.
N = 30
20
18
16
14
12
10
•What happens if N is the
same as the number of
samples in x[n]? To find out,
set N1 = 30. What does the
resulting plot look like? Why
does it look like this?
8
6
4
2
0
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
11
1
Example (2)
z
Example 2: In the last example the length of
x[n] was limited to 3 periods in length.
z
Now, let's choose a large value for N (for a
transform with many points), and vary the
number of repetitions of the fundamental period.
12
Example 2 (Continue)
n = [0:29];
x1 = cos(2*pi*n/10); % 3 periods
x2 = [x1 x1]; % 6 periods
x3 = [x1 x1 x1]; % 9 period
N = 2048;
X1 = abs(fft(x1,N));
X2 = abs(fft(x2,N));
X3 = abs(fft(x3,N));
F = [0:N-1]/N;
subplot(3,1,1)
plot(F,X1),title('3 periods'),axis([0 1 0 50])
subplot(3,1,2)
plot(F,X2),title('6 periods'),axis([0 1 0 50])
subplot(3,1,3)
plot(F,X3),title('9 periods'),axis([0 1 0 50])
13
Example 2 (continue)
z
z
z
z
The first plot, the transform of
3 periods of a cosine, looks
like the magnitude of 2 sincs
with the centre of the first sinc
at 0.1fs and the second at
0.9fs.
The second plot also has a
sinc-like appearance, but its
frequency is higher and it has
a larger
magnitude at 0.1fs and 0.9fs.
Similarly, the third plot has a
larger sinc frequency and
magnitude.
As x[n] is extended to an large
number of periods, the sincs
will begin to look more and
more like impulses.
3 periods
50
0
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
0.6
0.7
0.8
0.9
1
0.6
0.7
0.8
0.9
1
6 periods
50
0
0
0.1
0.2
0.3
0.4
0.5
9 periods
50
0
0
0.1
0.2
0.3
0.4
0.5
14
FT of a sinusoid wave
z
sinusoid transformed to an impulse, why do we have sincs in
thefrequency domain? When the FFT is computed with an N larger
than the number of samples in x[n], it ¯lls in the samples after x[n] with
zeros.
z
Example 2 had an x[n] that was 30 samples long, but the FFT had an
N = 2048. When Matlab computes the FFT, it automatically fills the
spaces from n = 30 to n = 2047 with zeros. This is like taking a
sinusoid and mulitipying it with a rectangular box of length 30. A
multiplication of a box and a sinusoid in the time domain should result
in the convolution of a sinc with impulses in the frequency domain.
z
Furthermore, increasing the width of the box in the time domain
should increase the frequency of the sinc in the frequency domain.
The previous Matlab experiment supports this conclusion.
15
Spectrum Analysis with the FFT and
Matlab
z
z
z
The FFT does not directly give you the spectrum of a
signal. As we have seen with the last two experiments, the
FFT can vary dramatically depending on the number of
points (N) of the FFT, and the number of periods of the
signal that are represented. There is another problem as
well.
The FFT contains information between 0 and fs, however,
we know that the sampling frequency must be at least twice
the highest frequency component.
Therefore, the signal's spectrum should be entirly below
fs/2 the Nyquist frequency.
16
Spectrum Analysis with the FFT and
Matlab
z
Furthermore, a real signal should have a transform
magnitude that is symmetrical for positive and
negative frequencies.
z
So instead of having a spectrum that goes from 0
to fs, it would be more appropriate to show the
spectrum from –fs/2 to fs/2
17
z
Spectrum Analysis with the FFT and
Matlab (Example 3)
This can be accomplished by using Matlab's fftshift
function as the following code demonstrates.
n = [0:149];
x1 = cos(2*pi*n/10);
N = 2048;
X = abs(fft(x1,N));
X = fftshift(X);
F = [-N/2:N/2-1]/N;
plot(F,X),
xlabel('frequency / f s')
18
Spectrum Analysis with the FFT and
Matlab (Example 3)
80
70
60
50
40
30
20
10
0
-0.5
-0.4
-0.3
-0.2
-0.1
0
0.1
frequency / f s
0.2
0.3
0.4
0.5
19
Correlation
z
Correlation is a mathematical tool used frequently
in signal processing for analysing functions or
series of values.
z
Correlation is the mutual relationship between two
or more signals.
z
Autocorrelation is the correlation of a signal with
itself. This is unlike cross-correlation, which is the
correlation of two different signals.
20
Cross-correlation
z
The typical syntax for computing the cross correlation of
two signals is:
[C Lags] = xcorr(x,y)
–
Where:
z C is the cross correlation sequence
z Lags is a vector of the lag indices at which c was estimated
z x and y are the two signals to be correlated
z
This Matlab built-in fuctions returns the cross-correlation
sequence in a length 2*N-1 vector, where x and y are
length N vectors (N>1).
z
If x and y are not the same length, the shorter vector is
zero-padded to the length of the longer vector
21
Example 4
z
Define two time-domain
signals as follows
clear all;
w=zeros(1000,1);
x=w;x(100:300)=1;
y=w;y(200:800)=1.5;
t=1:length(w);
figure(1);
plot(t,x,t,y);
legend ('x(t)','y(t)');
axis([0 1000 -1 2]);
title('Time domain signals');
xlabel('time, t in seconds');
Time domain signals
2
x(t)
y(t)
1.5
1
0.5
0
-0.5
-1
0
100
200
300
400
500
600
time, t in seconds
700
800
900
1000
22
Example 4 (Continue)
The cross correlation between the
previously defined two signals can be
obtained using the following code
cross correlation Function
350
–
–
–
–
–
[R L]=xcorr(y,x);
figure, plot(L,R);
title('cross correlation Function');
xlabel('time delay, \tau in seconds');
ylabel('R( \tau )');
300
250
200
R( τ )
z
150
100
50
0
-50
-1000
-800
-600
-400
-200
0
200
400
time delay, τ in seconds
600
800
23
1000
Auto-correlation
z
The typical syntax for computing the auto
correlation of two signals is:
[C Lags] = xcorr(x)
–
z
Where:
z C is the cross correlation sequence
z Lags is a vector of the lag indices at which c was
estimated
z x is the signal to be correlated
This Matlab built-in fuctions returns the autocorrelation sequence in a length 2*N-1 vector,
where x is a length N vector (N>1).
24
Example 5
z
Define three time-domain
signals as follows
–
–
–
–
–
–
–
–
–
–
–
w=zeros(1000,1);
w1=w;w1(400:600)=1;
w2=w;w2(200:800)=1.01;
w3=w;w3(100:900)=1.02;
t=1:length(w);
figure(1);
plot(t,w1,t,w2,t,w3);
legend
('w_1(t)','w_2(t)','w_3(t)');
axis([0 1000 -1 2]);
title('Time domain signals');
xlabel('time, t in seconds');
Time domain signals
2
w1(t)
w2(t)
1.5
w3(t)
1
0.5
0
-0.5
-1
0
100
200
300
400
500
600
time, t in seconds
700
800
900
25
1000
Example 5 (continue)
z
The three auto-correlation
functions of the previously
defined signals can be
obtained using the following
code
Autocorrelation Functions
900
R1(τ)
800
R2(τ)
700
R3(τ)
600
r1=xcorr(w1);
r2=xcorr(w2);
r3=xcorr(w3);
tau=1:length(r1);
figure(2);
plot(tau,r1,tau,r2,tau,r3);
legend ('R_1(\tau)','R_2(\tau)','R_3(\tau)');
title('Autocorrelation Functions');
xlabel('time delay, \tau in seconds');
500
400
300
200
100
0
-100
0
200
400
600
800 1000 1200 1400
time delay, τ in seconds
1600
1800
26
2000
ADC & DAC
z
To implement an ADC & DAC using simulink please
follow the instruction in the given matlab handout
27
Download