From Fourier Series to Analysis of Non-stationary Signals - X

27
Examples and MATLAB project Final MATLAB project From Fourier Series to Analysis of Non-stationary Signals - X prof. Miroslav Vlcek December 14, 2016 prof. Miroslav Vlcek Lecture 10

Transcript of From Fourier Series to Analysis of Non-stationary Signals - X

Page 1: From Fourier Series to Analysis of Non-stationary Signals - X

Examples and MATLAB projectFinal MATLAB project

From Fourier Series to Analysis ofNon-stationary Signals - X

prof. Miroslav Vlcek

December 14, 2016

prof. Miroslav Vlcek Lecture 10

Page 2: From Fourier Series to Analysis of Non-stationary Signals - X

Examples and MATLAB projectFinal MATLAB project

Contents

1 Examples and MATLAB project

2 Final MATLAB project

prof. Miroslav Vlcek Lecture 10

Page 3: From Fourier Series to Analysis of Non-stationary Signals - X

Examples and MATLAB projectFinal MATLAB project

Contents

1 Examples and MATLAB project

2 Final MATLAB project

prof. Miroslav Vlcek Lecture 10

Page 4: From Fourier Series to Analysis of Non-stationary Signals - X

Examples and MATLAB projectFinal MATLAB project

Transforms

• Fourier transform gives the spectrum of the wholetime-series

• Which is OK if the time-series is stationary. But what if it isnot?

• We need a technique that can march along a timeseriesand analyze spectral content in different time instants.

• Short Time Fourier Transform, Wavelet Transform

prof. Miroslav Vlcek Lecture 10

Page 5: From Fourier Series to Analysis of Non-stationary Signals - X

Examples and MATLAB projectFinal MATLAB project

STFT and non-stationarity signal

1 Remove the mean of a signal

2 Make moving average filtering

3 Do segmentation of a signal using window functions

4 Perform Fourier transform

5 Do not forget the interpolation

prof. Miroslav Vlcek Lecture 10

Page 6: From Fourier Series to Analysis of Non-stationary Signals - X

Examples and MATLAB projectFinal MATLAB project

Moving average filtering

• Assume we have a EEG signal corrupted with noise

• Set the mean to zero

• Apply moving average filter to the noisy signal (use filterorder=3 and 5)

• The higher filter order will remove more noise, but it willalso distort the signal more (i.e. remove the signal partsalso)

• So, a compromise has to be found (normally by trial anderror)

prof. Miroslav Vlcek Lecture 10

Page 7: From Fourier Series to Analysis of Non-stationary Signals - X

Examples and MATLAB projectFinal MATLAB project

Example 1: Moving average filtering

% moving average filteringclearload(’zdroj.mat’);x=EEG(3).Data(:,1);N=length(x);

% length of average window is 3for i=1:N-2,y3(i)=(x(i)+x(i+1)+x(i+2))/3;endy3(N-1)=(x(N-1)+x(N))/3;y3(N)=x(N)/3;

%length of average window is 5for i=1:N-4,y5(i)=(x(i)+x(i+1)+x(i+2)+x(i+3)+x(i+4))/5;end

prof. Miroslav Vlcek Lecture 10

Page 8: From Fourier Series to Analysis of Non-stationary Signals - X

Examples and MATLAB projectFinal MATLAB project

Example 1: Moving average filtering

y5(N-3)=(x(N-3)+x(N-2)+x(N-1)+x(N))/5;y5(N-2)=(x(N-2)+x(N-1)+x(N))/5;y5(N-1)=(x(N-1)+x(N))/5;y5(N)=x(N)/5;subplot(3,1,1), plot(x,’r’);title(’original EEG’)subplot(3,1,2), plot(y3,’g’);title(’EEG signal with averaging of length 3’)subplot(3,1,3), plot(y5,’b’);title(’EEG signal with averaging of length 5’)print -depsc figureEEG

prof. Miroslav Vlcek Lecture 10

Page 9: From Fourier Series to Analysis of Non-stationary Signals - X

Examples and MATLAB projectFinal MATLAB project

Example 1: Moving average filtering

0 500 1000 1500−400

−200

0

200

0 500 1000 1500−400

−200

0

200

0 500 1000 1500−400

−200

0

200

original EEG

EEG signal with averaging of length 3

EEG signal with averaging of length 5

prof. Miroslav Vlcek Lecture 10

Page 10: From Fourier Series to Analysis of Non-stationary Signals - X

Examples and MATLAB projectFinal MATLAB project

Example 2: Meyer wavelets

% Set effective support and grid parameters.lb = -8; ub = 8; n = 1024;% Meyer wavelet and scaling functions.[phi,psi,x] = meyer(lb,ub,n);subplot(211)plot(x,psi), gridtitle(’Meyer wavelet’)subplot(212)plot(x,phi), gridtitle(’Meyer scaling function’)

prof. Miroslav Vlcek Lecture 10

Page 11: From Fourier Series to Analysis of Non-stationary Signals - X

Examples and MATLAB projectFinal MATLAB project

Example 2: Meyer wavelets

−8 −6 −4 −2 0 2 4 6 8−1

−0.5

0

0.5

1

1.5Meyer wavelet

−8 −6 −4 −2 0 2 4 6 8−0.5

0

0.5

1

1.5Meyer scaling function

Figure: Meyer wavelets

prof. Miroslav Vlcek Lecture 10

Page 12: From Fourier Series to Analysis of Non-stationary Signals - X

Examples and MATLAB projectFinal MATLAB project

Example 3: Discrete Wavelet Transform

• 1 Load in EEG signal withload(’zdroj.mat’);name=’dmey’ ;name2=’db1’ ;signal=EEG(3).Data(:,1);s=signal; ls = length(s);

• 2 The Discrete Wavelet Transform is provided by MATALB undera variety of extension modes. We can perform a one-stagewavelet decomposition of the signal for example with[ca1,cd1] = dwt(s,name);

• 3 Here ca1 is a vector of the approximation coefficient and cd1the detailed coefficient.

• 4 You can plot these coefficients.

prof. Miroslav Vlcek Lecture 10

Page 13: From Fourier Series to Analysis of Non-stationary Signals - X

Examples and MATLAB projectFinal MATLAB project

Example 3: Discrete Wavelet Transform

• 5 Perform one step reconstruction of ca1 and cd1. a1 =upcoef(’a’,ca1,name,1,ls);d1 = upcoef(’d’,cd1,name,1,ls);

• 6 Then you can plot the EEG signal approximation and invertdirectly decomposition of EEG signal using coefficients a0 =idwt(ca1,cd1,’db1’,ls);

prof. Miroslav Vlcek Lecture 10

Page 14: From Fourier Series to Analysis of Non-stationary Signals - X

Examples and MATLAB projectFinal MATLAB project

Example 3: Discrete Wavelet Transform

0 500 1000 1500−400

−200

0

200

0 500 1000 1500−400

−200

0

200approximation a1

0 500 1000 1500−100

−50

0

50

100detail d1

prof. Miroslav Vlcek Lecture 10

Page 15: From Fourier Series to Analysis of Non-stationary Signals - X

Examples and MATLAB projectFinal MATLAB project

Example 3: Discrete Wavelet Transform

0 500 1000 1500−500

0

500approximation a3

0 500 1000 1500−50

0

50detail d3

0 500 1000 1500−50

0

50detail d2

0 500 1000 1500−100

0

100detail d1

prof. Miroslav Vlcek Lecture 10

Page 16: From Fourier Series to Analysis of Non-stationary Signals - X

Examples and MATLAB projectFinal MATLAB project

Some preliminary info about EEG

Figure: Brain waves within 0-30 Hz

prof. Miroslav Vlcek Lecture 10

Page 17: From Fourier Series to Analysis of Non-stationary Signals - X

Examples and MATLAB projectFinal MATLAB project

MATLAB project

1 Develope file for computing STFT and wavelet transformfor an EEG signal

prof. Miroslav Vlcek Lecture 10

Page 18: From Fourier Series to Analysis of Non-stationary Signals - X

Examples and MATLAB projectFinal MATLAB project

Step 1: Load signal, input parameters

N = 128; okno = hanning( N+1);okno = okno( 1:N); fs = 1;prekryv = N-1; lupa = 64;zacatek = 1; delka = 1000;load(’zdroj.mat’);signal=EEG(3).Data(:,1);

prof. Miroslav Vlcek Lecture 10

Page 19: From Fourier Series to Analysis of Non-stationary Signals - X

Examples and MATLAB projectFinal MATLAB project

Step 2: FFT and moving average

f =128*(1:1024)/2048;aX = abs(fft(signal,2048));signal = signal( zacatek: zacatek+delka-1);

% here include moving averagedisp(’Computing spectrum...’)X = rozsekej( signal, N); % SegmentationX = diag( okno) * X; % Windowing[r,c] = size( X);X = [X; zeros( lupa, c)];

prof. Miroslav Vlcek Lecture 10

Page 20: From Fourier Series to Analysis of Non-stationary Signals - X

Examples and MATLAB projectFinal MATLAB project

Step 3: DFT, Spectrogram

spektrum = fft( X); % spectrumspektrum = spektrum( 1:64, :);

% the first 64 componentsRS = real( spektrum); IS = imag( spektrum);[numbercomponents, numbersamples] = size( RS);figure(1); colormap( jet);subplot(311)plot(signal); grid; title(’Original signal’);subplot(312)plot(f,aX(1:1024),’LineWidth’,2,’color’,[0 0 1]);title(’Frequency content’)subplot(313)imagesc( abs( spektrum)); title(’STFT’);print -depsc figure1EEG

prof. Miroslav Vlcek Lecture 10

Page 21: From Fourier Series to Analysis of Non-stationary Signals - X

Examples and MATLAB projectFinal MATLAB project

Signal with 50 Hz noise

0 100 200 300 400 500 600 700 800 900 1000−400

−200

0

200Original signal

0 10 20 30 40 50 60 700

1

2

3x 10

5 Frequency content

STFT

100 200 300 400 500 600 700 800 900 1000

20

40

60

prof. Miroslav Vlcek Lecture 10

Page 22: From Fourier Series to Analysis of Non-stationary Signals - X

Examples and MATLAB projectFinal MATLAB project

Signal without 50 Hz noise

0 100 200 300 400 500 600 700 800 900 1000−400

−200

0

200signal with 50 Hz noise reduced

0 10 20 30 40 50 60 700

1

2

3x 10

5 Fourier transform

STFT

100 200 300 400 500 600 700 800 900 1000

20

40

60

prof. Miroslav Vlcek Lecture 10

Page 23: From Fourier Series to Analysis of Non-stationary Signals - X

Examples and MATLAB projectFinal MATLAB project

Step 4: Wavelet decomposition

clear% Compute WT of an EEG signalload(’zdroj.mat’);signal=EEG(3).Data(:,1);ls=length(signal);name=’dmey’ ;[ca1,cd1] = dwt(signal,name);a1 = upcoef(’a’,ca1,name,1,ls);

prof. Miroslav Vlcek Lecture 10

Page 24: From Fourier Series to Analysis of Non-stationary Signals - X

Examples and MATLAB projectFinal MATLAB project

Step 4: A typical scalogram of EEG

signal=a1;wlen=64; Bsup=1; Logsc=0;wavtype = ’gaus4’;wavtype2 = ’mexh’;figure(1);h = msgbox( ’Wavelet transform

computation...’,’Please wait’);W = cwt(signal, 1:wlen/2, wavtype,’abslvl’);close( h);WW = abs( W);

prof. Miroslav Vlcek Lecture 10

Page 25: From Fourier Series to Analysis of Non-stationary Signals - X

Examples and MATLAB projectFinal MATLAB project

Step 4: A typical scalogram of EEG

if Bsup == 1, WW = WW - min( min ( WW)); end;if Logsc == 1, WW = log10( WW+eps); end;subplot(2,1,1)plot(signal,’LineWidth’,1.0,’color’,[1 0 0])subplot(2,1,2)imagesc(WW); colormap(jet);

prof. Miroslav Vlcek Lecture 10

Page 26: From Fourier Series to Analysis of Non-stationary Signals - X

Examples and MATLAB projectFinal MATLAB project

A lorry passing in front of a microphone

0 1 2 3 4 5 6−0.8

−0.6

−0.4

−0.2

0

0.2

0.4

0.6Orange lorry.wav

time(sec)

prof. Miroslav Vlcek Lecture 10

Page 27: From Fourier Series to Analysis of Non-stationary Signals - X

Examples and MATLAB projectFinal MATLAB project

MATLAB project - frequency-time stamp of a vehicle

• In order to consult your noise measurement and compiling thefinal project, we will meet on Tuesday, January 4, 2017. Do nothesitate to contact me any time later.

• I expect your project on a noise analysis for a real objects -vehicles, engines, ... to be delivered by February 17th .

• Merry Christmas!

prof. Miroslav Vlcek Lecture 10