You are on page 1of 13

,

2
MATLAB

. 2010-11

MATLAB. MATLAB (www.mathworks.com)


(Windows, Linux, Unix)
. () R2009a. MATLAB
https://cloudfront0.central.ntua.gr/sgd/hierarchy.jsp ()
( ).
R2010b Linux.
.
, labuser
labuser .
, Alt+Ctrl+Del. ,
Windows XP.

1:
:
,
. , DFT,
.
(.. ). , ,
(.. ). .

. y[n]
y(t),
x[n], 0n-1 x[n]=y[n]wr[n]
1 0 n N 1
wr [n ] =

0
. Fourier Y() y(t)
Fourier Y(f) . DTFT X() DTFT Y()
X ( ) = Y ( ) Wr ( ) , Wr() DTFT wr[n], ,
N 1

Wr ( ) = 1 exp( j 2 n ) =
n =0

D( N , ) 

1 exp( jN )
N 1
= exp j
D( N , )
1 exp( j )
2

sin( N / 2)
sin( / 2)
1

. 2010-11

Dirichlet (Dirichlet kernel). Dirichlet D(L,)


sinc, .

N 1/N
, 2/N. , 13 dB ( N). N
.
Dirichlet , , . ,
( DTFT) (smearing)
, , (leakage),
.
, ,
.
1.1
MATLAB
. >> .
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 1 - DFT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
%
clear all
%
close all
%
clc
%
%
% 0.25 Hz
%
L=32;
%
Fs=1;
% 1 Hz
Ts=1/Fs;
%
T=L*Ts;
% 32 sec
n=[0:Ts:T-Ts];
%
A=1;
%
phi=0;
%
f=0.25;
% (/)
x=A*cos(2*pi*n*f ...%
+phi); % 1 sec
2

. 2010-11

X=fft(x);
%
%
%
%
figure(1);
%
subplot(3,1,1); % 3x1
% 1
plot(n,x,'*k'); % (*) (k)
% help plot xyz
% ,
%
pause
%
%
hold on;
%
%
t=0:0.1:T;
% 0.1 sec
plot(t,A*cos(2*pi*f*t+phi),'-b'); % (-)
% (b)
grid off;
%
title('Sinusoid at 1/4 the Sampling Rate'); %
xlabel('Time (samples)');
% x
ylabel('Amplitude');
% y
hold off;
%
%
pause
%
%
%
magX=abs(X);
%
N=length(X);
% FFT (=L )
fn=[0:1/N:1-1/N];
%
subplot(3,1,2);
% 3x1
% 2
stem(fn,magX,'ok'); % ,
% x
grid on;
%
xlabel('Normalized Frequency (cycles per sample))');
ylabel('Magnitude (Linear)');
pause
%
% (dB):
%
spec=20*log10(magX);
% dB
subplot(3,1,3);
% 3x1
% 3
plot(fn,spec,'--sr');
% (s)
% (--) (r)
axis([0 1 -300 30]);
% -300 30 dB
grid on;
%
xlabel('Normalized Frequency (cycles per sample))');
ylabel('Magnitude (dB)');

M-file (My Documents\MATLAB).


lab2_1_nnnnn.m, nnnnn
.
1: plot(n,x,'-b') plot(t,A*cos(2*pi*f*t+phi),'-b'); lab2_nnnnn.txt, nnnnn ,
Notepad Windows (Start Programs Accessories
Notepad) My Documents. 2

. 2010-11

, ,
.
1.2
1.1
.

figure(2);

f=0.25+0.5/L;

% 0.25 (/)

title('Sinusoid NEAR 1/4 the Sampling Rate'); %

axis([0 1 0 30]);

% 0 30 dB

M-file (My
Documents\MATLAB). lab2_2_nnnnn.m, nnnnn
.
1.3


.
. . Fourier
(DFT) . 1
.
1.1
.

zpf=10;
X=fft(x,zpf*L);

%
%

plot(fn,magX,'-k'); %

spec = max(spec,-30*ones(1,length(spec))); % -30 dB

plot(fn,spec,'-r');
grid on;

%
%

axis([0 1 -30 30]);

% -30 30 dB

M-file (My
Documents\MATLAB). lab2_3_nnnnn.m, nnnnn
.
1.4
1.1 2

. 2010-11

1.3. 1.2 .
2: 1.1 1.2; lab2_nnnnn.
1.5
M-file (My
Documents\MATLAB). lab2_4_nnnnn.m, nnnnn
:
1. 2 .
2. Browse.
3. (My Documents\MATLAB) .
4. .
5. , .
6.
.

2:
DTFT W()
.
. dB
.
(drop-off rate).
.
Matlab .
doc window . .

, L . (DTFT) . 2/L.
-13 dB -6 dB .
2

. 2010-11

Rectangular window kernel


0

N orma lized Mag nitu de (d B)

-5
-10
-15
-20
-25
-30
-35
-40
-45
-1

-0.8

-0.6

-0.4

-0.2
0
0.2
Normalized Frequency

0.4

0.6

0.8

Bartlett
.
L,
2n
0 n N /2
N
w[n ] =
2 2n N / 2 n N

N
N=L-1. Bartlett .
4/(L+1), , . -27 dB -12 dB (
).
Bartlett window kernel

Norma lized Mag nitud e (dB)

-20

-40

-60

-80

-100
-1

-0.8

-0.6

-0.4

-0.2
0
0.2
Normalized Frequency

0.4

0.6

0.8

. 2010-11

Hann Hanning
.
L,
n

w[n ] = 0.5 1 cos 2 , 0 n N


N

N=L-1. Hann .
4/L, , .
-32 dB -18 dB ( Bartlett).
Hann window kernel
0

Norma lized Mag nitud e (dB)

-20
-40
-60
-80
-100
-120
-140
-1

-0.8

-0.6

-0.4

-0.2
0
0.2
Normalized Frequency

0.4

0.6

0.8

Hamming
Hann .
L,
n

w[n ] = 0.54 0.46cos 2 , 0 n N


N

N=L-1. Hamming . 4/L, , .


-43 dB ( Hann) -6 dB
( ).

. 2010-11

Hamming window kernel

Norma lized Mag nitud e (dB)

-20

-40

-60

-80

-100
-1

-0.8

-0.6

-0.4

-0.2
0
0.2
Normalized Frequency

0.4

0.6

0.8

Blackman
Hann
. L,

n
n

w[n ] = 0.42 0.5cos 2 + 0.08cos 2 , 0 n N


N
N

N=L-1. Blackman . 6/L, , .


-57 dB -18 dB ( Hamming).
Blackman window kernel
0

Norma lized Mag nitud e (dB)

-20
-40
-60
-80
-100
-120
-140
-1

-0.8

-0.6

-0.4

-0.2
0
0.2
Normalized Frequency

0.4

0.6

0.8

. 2010-11

Matlab rectwin, bartlett, hann,


blackman, . , wintool

1 . wintool Matlab
Start Toolboxes Signal Processing Window Design & Analysis Tool (wintool).
hamming

2.1
1.1 1.2 Blackman . :

w=blackman(L);

% Blackman

X=fft(x.*w');

%
% w
%

axis([0 1 -60 20]);

% -60 20 dB

2.2
M-file lab2_5_nnnnn.m, nnnnn
1.5.

3:
(Power Spectral Density PSD) Fourier .
N
1
2
S X ( f ) = lim E
x[n ]exp( j 2 nf

N
2 N + 1 n = N

Wiener-Khinchin.
(ensemble average).
. , Fourier
, x[n] . ,
PSD ,
DFT , ,
DFT.
PSD.
1
(periodogram). DTFT
. L x[n]

wintool 32 . .. 41 64 ( View Analysis Parameters).


2

Pxx ( f ) =

1
Lf s

L 1

x[n]exp ( j 2 nf / f )

. 2010-11

n =0


fk=kfs/, k=0, 1, , FFT,
2
1 N 1
Pxx ( f k ) =
x[n ]exp ( j 2 nk / N ) , k = 0,1,..., N 1

Lf s n =0
N>L, FFT L .
(biased estimator) PSD.
f /2
2
1 s sin ( L ( f f ) / f s )
E {Pxx ( f )} =
S X ( f )df
Lf s fs / 2 sin 2 ( ( f f ) / f s )
, PSD Dirichlet.

Bartlett.
27 dB
. , L, PSD Bartlett .
, L
.

. .

.
fs/L.
(bias) . DFT.
, , .

(, ). . , .

. ,
.
U=

1
N

N 1

w[n]

n =0

.
, . periodogram Matlab . help .
2

10

. 2010-11

.
(ensemble average). Bartlett
K L,
PSD.
.

. Welch PSD
. ( ) ,
PSD. pwelch Matlab,
, 8 50% Hamming .
,
(
). , . . . . Welch , ,
. ,
Welch . pwelch Matlab
Welch. help
.
3.1
MATLAB
-10 dBW.
>> .
fs, 0/2,
-fs/2 fs/2 0/2fs = 0.1, 0 = 0.2/8000 -46 dB/Hz
.
%
%
%
close all; clear all;
Fs=8000;
%
t=0:1/Fs:1;
% 1 sec
L=length(t);
n=wgn(1,L,-10);
%
Pn= sum(n.^2)/length(n) % 0.1 (-10 dBW)
%
%
%
figure (1); periodogram(n,[],[],Fs)
%
figure (2); pwelch(n,[],[],[],Fs);
% Welch

. wgn .
2

11

. 2010-11

3: Welch;
lab2_nnnnn.
4: 2 sec,
; lab2_nnnnn.
3.2 Bartlett

. >> . N=100 1000 lab2_6_nnnnn.jpg, nnnnn
,
1.5.
%
%
%
close all; clear all;
fs = 256;
t = (0:fs)/fs;
n = 0.1*randn(size(t));
%
%
%
[Pxx,f]=periodogram(n,[],[],fs);

%
% 1 sec
% 0.01

%
%
%
%

Pxx
f

doc periodogram

N=10;
for i=1:N-1
% -1
n = 0.1*randn(size(t));
Pxx=Pxx+periodogram(n,[],[],fs);
end
figure;plot(f,10*log10(Pxx/N))
xlabel('Frequency (Hz)')
ylabel('Power Spectral Density (dB/Hz)')

5: randn(size(t)), randn(length(t))
;
lab2_nnnnn.
6: 0 ;
lab2_nnnnn.
3.3
MATLAB
Welch
30 dB
M-file lab2.m.
%
%
%
2

12

. 2010-11

close all; clear all;


fs = 500;
t = (0:fs)/fs;
A = [1 2 0.5];
f = [120;150;200];
x = A*sin(2*pi*f*t);
xn = awgn(x,30,'measured');

%
%
%
%
%
%
%


1 sec
( )
( )

SNR=30 dB
help awgn

%
%
%
figure (1); periodogram(xn,[],[],fs)
figure (2); pwelch(xn,[],[],[],fs);

,
Welch.
7: x = A*sin(2*pi*f*t) ; lab2_nnnnn.
8: 0
awgn; lab2_nnnnn.
3.4
lab2.m,
.m.
. [140;150;200] [145;150;200]
140;160;200.
9: ;
lab2_nnnnn.
3.5
1.
2.
3.

lab2_nnnnn.txt
1.5.
.
, .

13

You might also like