<<

Fast Transform and MATLAB Implementation

by Wanjun Huang for Dr. Duncan L. MacFarlane

1 Signals

In the fields of communications, , and in more generally, a signal is any time‐varying or spatial‐varying quantity This (quantity) changes in time • Speech or audio signal: A sound that varies in time • Temperature readings at different hours of a day • Stock price changes over days • Etc. Signals can be classified by continues‐time signal and discrete‐time signal: • A discrete signal or discrete‐time signal is a time , perhaps a signal that has been sampldled from a continuous‐time siilgnal • A digital signal is a discrete‐time signal that takes on only a discrete set of values Continuous Time Signal Discrete Time Signal 1 1

0.5 0.5

0 0 f(t) f[n]

-0.5 -0.5

-1 -1 0 10 20 30 40 0 10 20 30 40 Time (sec) n

2 Periodic Signal periodic signal and non‐periodic signal: Periodic Signal Non-Periodic Signal 1 1

0

f(t) 0 f[n]

-1 -1 0 10 20 30 40 0 10 20 30 40 Time (sec) n • Period T: The minimum interval on which a signal repeats

• Fundamental : f0=1/T • Harmonic : kf0 • Any periodic signal can be approximated

by a sum of many sinusoids at harmonic frequencies of the sig(gnal(kf0) with appropriate amplitude and • Instead of using sinusoid signals, mathematically, we can use the complex exponential functions with both positive and negative harmonic frequencies Euler formula: exp( j t )  sin(  t )  j cos(  t )

3 Time‐Frequency Analysis • A signal has one or more frequencies in it, and can be viewed from two different standpoints: and

Time Domian Domian (Banded (Banded Wren Wren Song) Song) Frequency Domain 1

2

0 1 mplitude Power AA

-1 0 0 2 4 6 8 0 200 400 600 800 1000 1200 4 Sample x 10 Frequency (Hz) Time‐domain figure: how a signal changes over time Frequency‐domain figure: how much of the signal lies within each given frequency band over a range of frequencies Why frequency domain analysis? • To decompose a complex signal into simpler parts to facilitate analysis • Differential and difference equations and operations in the time domain become algebraic operations in the frequency domain • Fast (FFT)

4

We can go between the time domain and the frequency domain by using a tool called Fourier transform • A Fourier transform converts a signal in the time domain to the frequency domain(spectrum) • An inverse FFiourier transform converts the frequency ddiomain components back into the original time domain signal

Continuous‐Time Fourier Transform:      j t 1 j t F ( j )   f (t )e dt f (t )   F ( j )e d    2    Discrete‐Time Fourier Transform(DTFT): 

 1 j j n X (e j )   x[ n ]e  j n x[ n ]   X (e )e d  2  n   2  

5 Fourier Representation For Four Types of Signals

The signal with different time‐domain characteristics has different freqqyuency‐domain characteristics

1Continues‐time periodic signal ‐‐‐> discrete non‐periodic spectrum 2Continues‐time non‐periodic signal ‐‐‐> continues non‐periodic spectrum 3 Discrete non‐periodic signal ‐‐‐> continues periodic spectrum 4Discrete periodic signal ‐‐‐> discrete periodic spectrum

The last transformation between time‐domain and freqqyuency is most useful The reason that discrete is associated with both time‐domain and frequency domain is because computers can only take finite discrete time signals

6 Periodic A periodic sequence with period N is defined as: ~x (n )  ~x (n  kN ) , where k is

2  j kn kn N For example: W N  e (it is called Twiddle Factor)

kn ( k  N ) n k ( n  N ) Properties: Periodic W N  W N  W N

 kn kn * ( N  k ) n k ( N  n ) Symmetric W N  (W N )  W N  W N

N 1 kn  N n  rN Orthogonal  W N   k  0  0 other

For a periodic sequence with period N, only N samples x(n) are independent. So that N sample in one period is enough to represent the whole sequence

7 Discrete (DFS)

Periodic signals may be expanded into a series of and cosine functions N 1 ~ ~ kn X ( k )   x ( n )W N ~ ~ n  0 X (k )  DFS ( x (n )) N 1 ~ ~ ~ 1 ~  kn x (n )  IDFS ( X (k )) x ( n )   X ( k )W N N n  0 ~ X ( k ) is still a periodic sequence with period N in frequency domain The Fourier series for the discrete‐time periodic shown below: Sequence x (in time domain) Fourier Coeffients 1 0.2

0.5 0 X -0.2 Amplitude

0 -040.4 010203040 0 10 20 30 40 time

8 Finite Length Sequence

Real lift signal is generally a  x ( n ) 0  n  N  1 fin ite lhlength sequence x ( n )    0 others

 If we periodic extend it by the period N, then ~x (n)   x(n  rN )  x(n)

~x(n)



9 Relationship Between Finite Length Sequence and PPidieriodic Sequence A periodic sequence is the periodic extension of a finite length sequence ~  x ( n )   x ( n  rN )  x (( n )) N m   A finite length sequence is the principal value interval of the periodic sequence ~ 1 0  n  N 1 x(n)  x(n)RN (n) Where RN (n)   0 others So that: ~ ~ x(n)  x (n)RN (n)  IDFS[X (k)]RN (n) ~ ~ X (k)  X (k)RN (k)  DFS[x (n)]RN (n)

10 Discrete Fourier Transform(DFT)

• Using the Fourier series representation we have Discrete Fourier Transform(DFT) for finite length signal • DFT can convert time‐domain discrete signal into frequency‐ domain discrete spectrum

N 1 Assume that we have a signal { x[ n ]} n  0 . Then the DFT of the signal is a sequence X [ k ] for k  0, , N  1

N 1 X [ k ]   x[ n ]e  2 jnk / N n  0 The Inverse Discrete Fourier Transform(IDFT): N 1 1 2  jnk / N x[ n ]   X [ k ]e , n  0,2, , N  1 . N k  0 Note that because MATLAB cannot use a zero or negative indices, the index starts from 1 in MATLAB 11 DFT Example

The DFT is widely used in the fields of spectral analysis, acoustics, medical imaging, and telecommunications. Time domain signal 6 For example: 5 4 ee x[ n ]  [ 2 4  1 6 ], N  4, ( n  0,1,2,3) 3 2  Amplitud 1 3  j nk 3 nk X [ k ]   x[ n ]e 2   x[ n ](  j ) 0 n  0 n  0 -1 0 0.5 1 1.5 2 2.5 3 Time X [0 ]  2  4  (  1)  6  11 Frequency domain signal 12 X [1] 2 ( 4 j ) 1 6 j 3 2 j        10

X [ 2 ]  2  (  4 )  (  1)  6   9 8

X [3]  2  ( 4 j )  1  6 j  3  2 j 6 |X[k]| 4

2

0 0 0.5 1 1.5 2 2.5 3 Frequency 12 (FFT)

• The Fast Fourier Transform does not refer to a new or different type of Fourier transform. It refers to a very efficient algorithm for computing the DFT • The time taken to evaluate a DFT on a computer depends principally on the number of involved. DFT needs 2 N multiplications. FFT only needs Nlog2(N) • The central insight which leads to this algorithm is the realization that a discrete Fourier transform of a sequence of N points can be written in terms of two discrete Fourier transforms of length N/2 • Thus if N is a , it is possible to recursively apply this decomposition until we are left with discrete Fourier transforms of single points

13 Fast Fourier Transform(cont.)

N 1 N 1  2 jnk / N nk Re‐writing X [ k ]   x[ n ]e as X [ k ]   x[ n ]W N n  0 n  0 nk It is easy to realize that the same values of W N are calculated many times as the computation proceeds Using the symmetric property of the twiddle factor, we can save lots of computations

N 1 N 1 N 1 nk kn kn X [ k ]   x[ n ]W N   x ( n )W N   x ( n )W N n  0 n  0 n  0 even n odd n N 2 1 N 2 1 2 kr k ( 2 r 1)   x ( 2 r )W N   x ( 2 r  1)W N r  0 r  0 N 2 1 N 2 1 kr k kr   x1 ( r )W N 2 W N  x 2 ( r )W N 2 r  0 r  0 k  X 1 ( k )  W N X 2 ( k ) Thus the N‐ DFT can be obtained from two N/2‐point transforms, one on even input data, and one on odd input data.

14 Introduction for MATLAB

MATLAB is a numerical computing environment developed by MathWorks. MATLAB allows manipp,ulations, ppglotting of functions and data, and implementation of

Getting help You can get help by typing the commands help or lookfor at the >> prompt, e.g. >> help fft Arithmetic operators Symbol Example + Addition 3.1 + 9 ‐ Subtraction 6.2 – 5 * 2 * 3 / Division 5 / 2 ^ Power 3^2 15 Data Representations in MATLAB

Variables:Variablesaredefinedastheassignmentoperator“=”.Thesyntaxof variable assignment is varibliable name = a value (or an expressi)ion) For example, >> x = 5 x= 5 >> y = [3*7, /3]; % pi is in MATLAB

Vectors/Matrices: MATLAB can create and manipu late arrays of 1 (ectors)(vectors), 2 (matrices), or more dimensions row vectors: a = [1, 2, 3, 4] is a 1X4 matrix column vectors: b = [5; 6; 7; 8; 9] is a 5X1 matrix, e.g. >>A=[123;789;456] A = 123 789 4 5 6

16 Mathematical Functions in MATLAB

MATLAB offers many predefined mathematical functions for technical comppg,uting, e.g.

cos(x) Cosine abs(x) sin(x) Sine (x) Phase angle exp(x) Exponential conj(x) sqrt(x) root log(x)

Colon (:) Suppose we want to enter a vector x consisting of points (0,0.1,0.2,0.3,…,5). We can use the command >> x = 0:0.1:5;

Most of the work you will do in MATLAB will be stored in files called scripts,orm‐files, containing of MATLAB commands to be executed over and over again

17 Basic plotting in MATLAB

MATLAB has an excellent set of graphic tools. Plotting a given data set or the results of computation is possible with very few commands The MATLAB command to plot a graph is plot(x,y), e.g. Sine 1

>> x = 0:pp/i/100:2* p;pi; 0.8 >> y = sin(x); 0.6 >> plot(x,y) 0.4

0.2 xx MATLAB enables you to add axis 0 Sine of of Sine Labels and titles, e.g. -0.2

-0.4

>> xlabel('x=0:2\pi'); -0.6 >> ylabel('Sine of x'); -0.8

-1 >> tile('Sine function') 0 1 2 3 4 5 6 7 xx0:2=0:2

18 Example 1:

Sine Wave Signal 1 Fs = 150; % Sampling frequency t = 0:1/Fs:1; % Time vector of 1 second 0.5 f = 5; % Create a sine wave of f Hz. x = sin (2*p i*t*f); 0 nfft = 1024; % Length of FFT % Take fft, padding with zeros so that length(X)

Amplitude is equal to nfft -0.5 X = fft(x,nfft); % FFT is symmetric, throw away second half -1 0 0.2 0.4 0.6 0.8 1 X = X(1:nfft/2); Time (s) % Take the magnitude of fft of x mx = abs(X); % Frequency vector Power Spectrum of a Sine Wave f = (0:nfft/2-1)*Fs/nfft; 80 % Generate the plot, title and labels. figure(1); 60 plot(t,x);

r title('Sine Wave Signal'); 40 xlabel('Time (s)'); owe

PP ylbllabel('Amp litu de '); 20 figure(2); plot(f,mx); title('Power Spectrum of a Sine Wave'); 0 0 10 20 30 40 50 60 70 80 xlabel('Frequency (Hz)'); Frequency (Hz) ylabel('Power');

19 Example 2: Cosine Wave

Cosine Wave Signal 1 Fs = 150; % Sampling frequency t = 0:1/Fs:1; % Time vector of 1 second 0.5 f = 5; % Create a sine wave of f Hz. x=x = cos(2*pi*t*f); nfft = 1024; % Length of FFT 0 % Take fft, padding with zeros so that length(X) is

Amplitude equal to nfft -0.5 X = fft(x,nfft); % FFT is syy,mmetric, throw awa y second half -1 X = X(1:nfft/2); 0 0.2 0.4 0.6 0.8 1 Time (s) % Take the magnitude of fft of x mx = abs(X); % Frequency vector Power Spectrum of a Cosine Wave f = (0:nfft/2-1)*Fs/nfft; 80 % Generate the plot, title and labels. figure(1); 60 plot(t,x); title('Sine Wave Signal'); r 40 xlabel('Time (s)'); owe

PP ylabel('Amplitude'); figure(2); 20 plot(f,mx); title('Power Spectrum of a Sine Wave'); 0 0 10 20 30 40 50 60 70 80 xlabel('Frequency (Hz)'); Frequency (Hz) ylabel('Power');

20 Example 3: Cosine Wave with Phase Shift

Cosine Wave Signal with Phase Shift 1 Fs = 150; % Sampling frequency t = 0:1/Fs:1; % Time vector of 1 second 0.5 f = 5; % Create a sine wave of f Hz. pha = 1/3*pi; % phase shift x = cos(2*pi*t*f + pha); 0 nfft = 1024; % Length of FFT

Amplitude % Take fft, padding with zeros so that length(X) is -0.5 equal to nfft X = fft(x,nfft); -1 % FFT is symmetric, throw away second half 0 0.2 0.4 0.6 0.8 1 Time (s) X = X(1:nfft/2); % Take the magnitude of fft of x mx = abs(X); Power Spectrum of a Cosine Wave Signal with Phase Shift % Frequency vector 80 f = (0:nfft/2-1)*Fs/nfft; % Generate the plot, title and labels. 60 figure(1); plot(t,x); r 40 title('Sine Wave Signal'); owe

PP xlabel('Time (s) '); ylabel('Amplitude'); 20 figure(2); plot(f,mx); 0 0 10 20 30 40 50 60 70 80 title('Power Spectrum of a Sine Wave'); Frequency (Hz) xlabel('Freqqyuency (Hz)'); ylabel('Power');

21 Example 4: Square Wave

Square Wave Signal Fs = 150; % Sampling frequency 1 t = 0:1/Fs:1; % Time vector of 1 second f = 5; % Create a sine wave of f Hz. 0.5 x = square(2*pi*t*f); nfft = 1024; % Length of FFT 0 % Take fft, padding with zeros so that length(X) is

Amplitude equal to nfft -0.5 X = fft(x,nfft); % FFT is syy,mmetric, throw awa y second half -1 X = X(1:nfft/2); 0 0.2 0.4 0.6 0.8 1 % Take the magnitude of fft of x Time (s) mx = abs(X); % Frequency vector Power Spectrum of a Square Wave f = (0:nfft/2-1)*Fs/nfft; 100 % Generate the plot, title and labels. figure(1); 80 plot(t,x);

r 60 title('Square Wave Signal'); xlabel('Time (s)');

Powe 40 ylabel('Amplitude'); figure(2); 20 plot(f,mx); 0 title('Power Spectrum of a Square Wave'); 0 20 40 60 80 xlabel('Frequency (Hz)'); Frequency (Hz) ylabel('Power');

22 Example 5: Square Pulse

Square Pulse Signal 1 Fs = 150; % Sampling frequency t = -0.5:1/Fs:0.5; % Time vector of 1 second 0.8 w = .2; % width of rectangle x=x = rectpuls(t, w); % Generate Square Pulse 0.6 nfft = 512; % Length of FFT % Take fft, padding with zeros so that length(X) is 0.4

Amplitude equal to nfft X = fft(x,nfft); 0.2 % FFT is syy,mmetric, throw awa y second half 0 X = X(1:nfft/2); -0.5 0 0.5 % Take the magnitude of fft of x Time (s) mx = abs(X); % Frequency vector Power Spectrum of a Square Pulse 30 f = (0:nfft/2-1)*Fs/nfft; % Generate the plot, title and labels. 25 figure(1); 20 plot(t,x); title('Square Pulse Signal'); 15 xlabel('Time (s)'); Power 10 ylabel('Amplitude'); figure(2); 5 plot(f,mx); 0 title('Power Spectrum of a Square Pulse'); 0 20 40 60 80 xlabel('Frequency (Hz)'); Frequency (Hz) ylabel('Power');

23 Example 6: Gaussian Pulse Gaussian Pulse Signal 4 Fs = 60; % Sampling frequency t = -.5:1/Fs:.5; 3 x = 1/(sqrt(2*pi*0.01))*(exp(-t.^2/(2*0.01))); nfft = 1024; % Length of FFT % Take fft, padding with zeros so that 2 length(X) is equal to nfft

Amplitude X = fft(x,nfft); 1 % FFT is symmetric, throw away second half X = X(()1:nfft/2); 0 % Take the magnitude of fft of x -0.5 0 0.5 mx = abs(X); Time (s) % This is an evenly spaced frequency vector f = (0:nfft/2-1)*Fs/nfft; % Generate the plot, title and labels. Power Spectrum of a Gaussian Pulse figure(1); 60 plot(t,x); title('Gaussian Pulse Signal'); 50 xlabel('Time (s)'); 40 ylabel('Amplitude'); rr figure(2); 30 plot(f,mx); Powe 20 title('Power Spectrum of a Gaussian Pulse'); xlabel('Frequency (Hz)'); 10 ylabel('Power');

0 0 5 10 15 20 25 30 Frequency (Hz)

24 Example 7: Exponential Decay

Exponential Decay Signal 2 Fs = 150; % Sampling frequency t = 0:1/Fs:1; % Time vector of 1 second 1.5 x = 2*exp(-5*t); nfft = 1024 ; % Length of FFT % Take fft, padding with zeros so that 1 length(X) is equal to nfft

Amplitude X = fft(x,nfft); 0.5 % FFT is symmetric, throw away second half 0 X = X(1:nfft/2); 0 0.2 0.4 0.6 0.8 1 % Take the magnitude of fft of x Time (s) mx = abs(X); % This is an evenly spaced frequency vector PStfEtilDSilPower Spectrum of Exponential Decay Signal f = (0:nfft/2-1)*Fs/nfft; 70 % Generate the plot, title and labels. 60 figure(1); 50 plot(t,x); title('Exponential Decay Signal'); 40 ere xlabel('Time (s)' ); 30 Pow ylabel('Amplitude'); 20 figure(2); plot(f,mx); 10 title('Power Spectrum of Exponential 0 Decayyg Signal'); 0 20 40 60 80 Frequency (Hz) xlabel('Frequency (Hz)'); ylabel('Power');

25 Example 8: Chirp Signal

Chirp Signal 1 Fs = 200; % Sampling frequency t = 0:1/Fs:1; % Time vector of 1 second x = chirp(t,0,1,Fs/6); 0.5 nfft = 1024;;g % Length of FFT % Take fft, padding with zeros so that 0 length(X) is equal to nfft X = fft(x,nfft); Amplitude -0.5 % FFT is symmetric, throw away second half X = X(1:nfft/2); % Take the magnitude of fft of x -1 0 0.2 0.4 0.6 0.8 1 mx = abs(X); Time (s) % This is an evenly spaced frequency vector f = (0:nfft/2-1)*Fs/nfft; Power Spppgectrum of Chirp Signal % Generate the plot, title and labels. 25 figure(1); 20 plot(t,x); title('Chirp Signal'); xlabel('Time (s)');

r 15 e

ww ylabel('Amplitude');

Po 10 figure(2); plot(f,mx); 5 title('Power Spectrum of Chirp Signal'); xlabel('Frequency (Hz)'); 0 ylabel('Power'); 0 20 40 60 80 100 Frequency (Hz)

26