PRACTICAL MATLAB® FOR ENGINEERS PRACTICAL MATLAB

(sharon) #1

DTFT, DFT, ZT, and FFT 531


MATLAB Solution
% Script file: shift
N=256; fs = 8000;Ts =1/fs; shift =32;
t = linspace(0,Ts*255,256);
window = [zeros(1,64) ones(1,64) zeros(1,64) ones(1,64)];
f = 7*cos(2*pi*1000*t);fn=f.*window;
DFT _ fn = fft(fn,256);
fn _ shift = 7*cos(2*pi*1000*(t-shift).*window);
DFT_fn_shift = fft(fn_shift,256);

figure(1)
subplot (3,1,1); % analysis of f(n) = 7cos(2.pi.1000.t)*window
plot (t,fn); ylabel(‘Magnitude’);
title (‘f(n) = 7cos(2.pi.1000.n)*window, analyzed in the time and
frequency domains’)
subplot (3,1,2)
W= [1:1:N/2].*fs/N;
DFT _ mag = abs(DFT _ fn);
DFT _ phase = angle(DFT _ fn);
DFT _ mage = DFT _ mag(1:1:128);
DFT _ phasee = DFT _ phase(1:1:128);
plot(W,DFT _ mage); ylabel(‘abs [ DFT[f(n)]]’);
subplot (3,1,3)
plot (W,DFT _ phasee);
ylabel (‘angle[ DFT [f(n)]]’); xlabel (‘ frequency in Hz’)

figure(2); % Analysis of 7cos(2.pi.(n-shift))
DFT_mags = abs(DFT_fn_shift);
DFT _ phases = angle(DFT _ fn _ shift);
DFT _ magess = DFT _ mag(1:1:128);
DFT _ phasess =DFT _ phase(1:1:128);
subplot (3,1,1)
plot (t,fn _ shift);
title(‘ f(n-32) analyzed in the time and frequency domains’)
ylabel(‘Amplitude in time’);
subplot(3,1,2)
plot(W,DFT _ magess);
ylabel(‘Magnitude DFT[f(n-32)]’);
subplot(3,1,3)
plot(W,DFT _ phasess);
xlabel(‘frequency in Hz ’); ylabel(‘Phase of DFT[f(n-32)]’);

figure(3) % error analysis
subplot(3,1,1)
error _ time = fn-fn _ shift;
plot(t, error _ time);
title(‘ Error analysis in the time and frequency domains ’)
ylabel(‘Magnitude in time’)
subplot(3,1,2)
error _ mag=abs(DFT _ fn)-abs(DFT _ fn _ shift);
b a r(W,a b s(er r or _ m ag(1:128)));yla b el(‘M ag n itu d e ’);
subplot(3,1,3)
error _ phase=angle(DFT _ fn)-angle(DFT _ fn _ shift);
bar(W,abs(error _ phase(1:128)));ylabel(‘Phase ’);
Free download pdf