% title: sine_wave.m % author: Ronald W. Bazillion % description: This script file generates a sine wave and plots % its energy spectrum using the FFT. The script also % plays the tone but is commmented out. clear NUM_OF_SAMPLES = 100; f = 1e3; %frequency fs = 8e3; %sampling frequency T = 1/fs; %sampling period; Time between samples n = 0:NUM_OF_SAMPLES-1; x = sin(2*pi*f*n*T); %generate the sine wave; sine(wnT) where w = 2*pi*f figure(1),plot(n*T,x),grid on,axis([0 1/f -1 1]),xlabel('Time (s)'),ylabel('amplitude'),title('Sine Wave'); %plot the sine wave for one cycle. %sound(x); %play the sound out on the speaker spect = fft(x); %compute the FFT of x spect = fftshift(spect); %center the spectrum of the FFT output f_axis = -(size(spect,2)/2):(size(spect,2)/2)-1; %set axis to -NUM_OF_SAMPLES/2 < SAMPLE < (NUM_OF_SAMPLES/2)-1 f_axis = f_axis*(1/(size(spect,2)*T)); %set frequency axis to -Fs/2 < F < (Fs/2)-1 axis (Hz) = K(1/NT); axis (radians) = K(2pi/NT) figure(2),stem(f_axis,abs(spect)),hold on,plot(f_axis,abs(spect),'-r'),grid on; %plot the frequency spectrum. xlabel('Frequency -Fs/2 < F < (Fs/2)-1'),ylabel('Magnitude'),title('Spectral Domain Plot'); zoom