Академический Документы
Профессиональный Документы
Культура Документы
Ex 5 part 2
f=100; %input frequency in Hertz
w=2*pi*f; %input frequency in radians
A=1; %signal amplitude
start_time=0; %time when simulation starts
end_time=2/f; %time when simulation ends in seconds
Fs=1000; %sampling frequency
Ts=1/Fs; %sampling interval
t=start_time:Ts:end_time; %the time vector
s1=A*sin(w*t) %compute waveform
%plot the sampled signal waveform
subplot(2,1,1);stem(t,s1) %plot the waveform
xlabel('seconds')
ylabel('amplitude') %label the axes
grid
%plot the signal spectrum
N=length(s1) %obtain number of sample points in input
fv=((0:N-2)/(Ts*(N-1))) %frequency vector with +ve frequencies
(ensure only N values)
f1=fft(s1,N-1) %do DFT/FFT
f1s=f1;
%f1s=fftshift(f1);
%fv=(0 N-2)/(Ts*(N-1)))-Fs/2
%frequency vector shift
%frequency scale to create ve val
ues
%(NB: symmetry property for DFT:
W(n)=W(n-k))
amp_f1s=abs(f1s)/((N-1)/2) %scale fft amplitude
subplot(2,1,2); plot(fv,amp_f1s) %plot scaled magnitude spectrum
%set the range to view
%change these values to get more readabl
e plots
Xmin=0; %lowest frequency to view
Xmax=200; %highest frequency to view only +ve half
of the spectrum
%spectrum is needed since its symmetrica
l
%lowest amplitude to view
Ymin=0;
Ymax=1.5*A;
axis([Xmin Xmax Ymin Ymax]);
grid
xlabel('frequency');
ylabel('magnitude'); %label the axes