Digital Signal Processing Reference
In-Depth Information
index_t
¼
[0:1:N-1];
f
¼
[0:1:N-1]*8000/N;
% Map frequency bin to frequency (Hz)
xf
¼
abs(fft(x))/N;
% Calculate amplitude spectrum
figure(1)
%Using Bartlett window
x_b
¼
x.*bartlett(N)
'
; % Apply triangular window function
xf_b
¼
abs(fft(x_b))/N; % Calculate amplitude spectrum
subplot(2,2,1);plot(index_t,x);grid
xlabel(
);
subplot(2,2,3); plot(index_t,x_b);grid
xlabel(
Time index n
); ylabel(
x(n)
'
'
'
'
Time index n
); ylabel(
Triangular windowed x(n)
);
'
'
'
'
subplot(2,2,2);plot(f,xf);grid;axis([0 8000 0 1]);
xlabel(
);
subplot(2,2,4); plot(f,xf_b);grid; axis([0 8000 0 1]);
xlabel(
'
Frequency (Hz)
'
); ylabel(
'
Triangular windowed Ak
'
);
figure(2)
% Generate the sine wave sequence
x
¼
2*sin(2000*pi*[0:1:100]*T); % Generate 101 2000-Hz samples.
% Apply the FFT algorithm
N
¼
length(x);
index_t
¼
[0:1:N-1];
f
¼
[0:1:N-1]*fs/N;
xf
¼
abs(fft(x))/N;
% Using Hamming window
x_hm
¼
x.*hamming(N)
'
;
Frequency (Hz)
); ylabel(
Ak (no window)
'
'
'
'
% Apply Hamming window function
xf_hm
¼
abs(fft(x_hm))/N;
% Calculate amplitude spectrum
subplot(2,2,1);plot(index_t,x);grid
xlabel(
'
Time index n
'
); ylabel(
'
x(n)
'
);
subplot(2,2,3); plot(index_t,x_hm);grid
xlabel(
'
Time index n
'
); ylabel(
'
Hamming windowed x(n)
'
);
subplot(2,2,2);plot(f,xf);grid;axis([0 fs 0 1]);
xlabel(
'
Frequency (Hz)
'
); ylabel(
'
Ak (no window)
'
);
subplot(2,2,4); plot(f,xf_hm);grid;axis([0 fs 0 1]);
xlabel(
'
Frequency (Hz)
'
); ylabel(
'
Hamming windowed Ak
'
);
figure(3)
% Generate the sine wave sequence
x
2*sin(2000*pi*[0:1:150]*T); % Generate 151 2-kHz samples
% Apply the FFT algorithm
N
¼
length(x);
index_t
¼
¼
[0:1:N-1];
f
[0:1:N-1]*fs/N;
xf
¼
2*abs(fft(x))/N;xf(1)
¼
xf(1)/2;
¼
% Single-sided spectrum
%Using Hanning window
x_hn
¼
x.*hanning(N)
'
;
xf_hn
¼
2*abs(fft(x_hn))/N;xf_hn(1)
¼
xf_hn(1)/2;
% Single-sided spectrum
subplot(2,2,1);plot(index_t,x);grid
xlabel(
'
Time index n
'
); ylabel(
'
x(n)
'
);
subplot(2,2,3); plot(index_t,x_hn);grid
xlabel(
'
Time index n
'
); ylabel(
'
Hanning windowed x(n)
'
);
subplot(2,2,2);plot(f(1:(N-1)/2),xf(1:(N-1)/2));grid;axis([0 fs/2 0 1]);
xlabel(
'
Frequency (Hz)
'
); ylabel(
'
Ak (no window)
'
);
subplot(2,2,4); plot(f(1:(N-1)/2),xf_hn(1:(N-1)/2));grid;axis([0 fs/2 0 1]);
xlabel(
'
Frequency (Hz)
'
); ylabel(
'
Hanning windowed Ak
'
);
Search WWH ::
Custom Search