Lab 2: Producing/Displaying Waveforms and Spectra of AM and FM Signals

Aok5
MatlabCode.m

%% Code for Lab1 of CEC 411 % JHL 8/27/15 % See Lab1 handouts for requirements clear all; close all; % Common parameter definitions: myID = 1234; % last four digits of my ID myID_str = num2str(myID); Nd = 1024; % d stands for dense sampling Ns = Nd /16; % s stands for sparse sampling Freq_1 = floor((10 + str2num(myID_str(1)))/3); % frequency of signal in Hz Fs_d = Nd; % Sampling rate for dense sampling Fs_s = Ns; % Sampling rate for sparse sampling t_d = (0:Nd-1)'/Fs_d; % Sampling times for dense sampling t_s = (0:Ns-1)'/Fs_s; % Sampling times for sparse sampling Freq_s = (-Fs_s/2 : Fs_s/2-1)'; % Frequencies 4 spectrum display Am = [1, 0.5, 0.2]; fm = [Freq_1, 2*Freq_1, 3*Freq_1]; pm = [0, pi/2, pi/3]; pha_a = floor(mod(myID, 1000)/100); pha_b = str2num(myID_str(2)); % Note: the above pha_a and pha_b have the same value from different eqns. %% Single sinusoid %% Task 1 1A x1_s = cos(2*pi*Freq_1*t_s + pha_a); x1_d = cos(2*pi*Freq_1*t_d + pha_b); figure(1); subplot(211); plot(t_s, x1_s, 'o', t_d, x1_d); axis([0 t_d(end) -1.5 +1.5]); xlabel('Time'); ylabel('Waveform'); title('Produced by JHL'); %% Task 1 1B subplot(212); X1_s = fft(x1_s); X1_F = abs(fftshift(X1_s)); stem(Freq_s, X1_F); text(Freq_1+1, abs(X1_s(Freq_1+1))*1.1, num2str(X1_s(Freq_1+1))); axis([Freq_s(1) Freq_s(end) 0 45]); xlabel('Freq'); ylabel('Spectrum'); %print('-dpdf', '../figs/fig1.pdf'); print('-depsc2', '../figs/fig1.eps'); %% Task 1 1C Freq_2 = 2 * Freq_1; x2_s = 0.5 * cos(2*pi*Freq_2*t_s + pha_a); x2_d = 0.5 * cos(2*pi*Freq_2*t_d + pha_b); figure(2); subplot(211); plot(t_s, x2_s, 'o', t_d, x2_d); axis([0 t_d(end) -1.5 +1.5]); xlabel('Time'); ylabel('Waveform'); title('Produced by JHL'); subplot(212); X2_s = fft(x2_s); %toLabe2 = num2str(X2_s(Freq_2+1)); X2_F = abs(fftshift(X2_s)); stem(Freq_s, X2_F); text(Freq_2+1, abs(X2_s(Freq_2+1))*1.1, num2str(X2_s(Freq_2+1))); axis([Freq_s(1) Freq_s(end) 0 45]); xlabel('Freq'); ylabel('Spectrum'); %print('-dpdf', '../figs/fig2.pdf'); print('-depsc2', '../figs/fig2.eps'); %% Multiple sinusoids %% Task 2 2A % Add the above two sinusoids directly xx1_s = x1_s + x2_s; xx1_d = x1_d + x2_d; x1_d = cos(2*pi*Freq_1*t_d + pha_b); figure(3); subplot(211); plot(t_s, xx1_s, 'o', t_d, xx1_d); axis([0 t_d(end) -1.5 +1.5]); xlabel('Time'); ylabel('Waveform'); title('Produced by JHL'); subplot(212); XX1_s = fft(xx1_s); XX1_F = abs(fftshift(XX1_s)); stem(Freq_s, XX1_F); [pks, locs] = findpeaks(abs(XX1_s(1:Ns/2)), 'MINPEAKHEIGHT', 1) for k = 1: length(locs) toLabel = num2str(XX1_s(locs(k))); text(locs(k), abs(XX1_s(locs(k)))*1.1, num2str(XX1_s(locs(k)))); end axis([Freq_s(1) Freq_s(end) 0 45]); xlabel('Freq'); ylabel('Spectrum'); %print('-dpdf', '../figs/fig3.pdf'); print('-depsc2', '../figs/fig3.eps'); %% Task 2 2B % Generate the sparse sequence using the iteration method xx2_s = zeros(size(t_s)); for m = 1:length(Am) xx2_s = xx2_s + Am(m) * cos(2*pi*fm(m)*t_s + pm(m)); end % Generate the dense sequence using the SUM method xx2_d = sum(repmat(Am, length(t_d), 1) .* ... cos(2*pi*t_d*fm + repmat(pm, length(t_d), 1)), 2); figure(4); subplot(211); plot(t_s, xx2_s, 'o', t_d, xx2_d); axis([0 t_d(end) -1.5 +1.5]); xlabel('Time'); ylabel('Waveform'); title('Produced by JHL'); subplot(212); XX2_s = fft(xx2_s); XX2_F = abs(fftshift(XX2_s)); stem(Freq_s, XX2_F); [pks, locs] = findpeaks(abs(XX2_s(1:Ns/2)), 'MINPEAKHEIGHT', 1) for k = 1: length(locs) toLabel = num2str(XX2_s(locs(k))); text(locs(k), abs(XX2_s(locs(k)))*1.1, num2str(XX2_s(locs(k)))); end axis([Freq_s(1) Freq_s(end) 0 45]); xlabel('Freq'); ylabel('Spectrum'); %print('-dpdf', '../figs/fig4.pdf'); print('-depsc2', '../figs/fig4.eps');