Matlab hw

% (ExampleFM.m) % This program uses triangl.m to illustrate frequency modulation % and demodulation ts=1.e-4; t=-0.04:ts:0.04; Ta=0.01; m_sig=triangl((t+0.01)/Ta)-triangl((t-0.01)/Ta); Lfft=length(t); Lfft=2^ceil(log2(Lfft)); M_fre=fftshift(fft(m_sig,Lfft)); freqm=(-Lfft/2:Lfft/2-1)/(Lfft*ts); B_m=100; %Bandwidth of the signal is B_m Hz. % Design a simple lowpass filter with bandwidth B_m Hz. h=fir1(80,[B_m*ts]); % kf=160*pi; m_intg=kf*ts*cumsum(m_sig); s_fm=cos(2*pi*300*t+m_intg); s_pm=cos(2*pi*300*t+pi*m_sig); Lfft=length(t); Lfft=2^ceil(log2(Lfft)+1); S_fm=fftshift(fft(s_fm,Lfft)); S_pm=fftshift(fft(s_pm,Lfft)); freqs=(-Lfft/2:Lfft/2-1)/(Lfft*ts); s_fmdem=diff([s_fm(1) s_fm])/ts/kf; s_fmrec=s_fmdem.*(s_fmdem>0); s_dec=filter(h,1,s_fmrec); % Demodulation % Using an ideal LPF with bandwidth Trange1=[-0.04 0.04 -1.2 1.2]; figure(1) subplot(211);m1=plot(t,m_sig); axis(Trange1); set(m1,'Linewidth',2); xlabel('{\it t} (sec)'); ylabel('{\it m}({\it t})'); title('Message signal'); subplot(212);m2=plot(t,s_dec); set(m2,'Linewidth',2); xlabel('{\it t} (sec)'); ylabel('{\it m}_d ({\it t})') title('demodulated FM signal'); figure(2) subplot(211);td1=plot(t,s_fm); axis(Trange1); set(td1,'Linewidth',2); xlabel('{\it t} (sec)'); ylabel('{\it s}_{\rm FM}({\it t})') title('FM signal'); subplot(212);td2=plot(t,s_pm); axis(Trange1); set(td2,'Linewidth',2); xlabel('{\it t} (sec)'); ylabel('{\it s}_{\rm PM}({\it t})') title('PM signal'); figure(3) subplot(211);fp1=plot(t,s_fmdem); set(fp1,'Linewidth',2); xlabel('{\it t} (sec)'); ylabel('{\it d s}_{\rm FM}({\it t})/dt') title('FM derivative'); subplot(212);fp2=plot(t,s_fmrec); set(fp2,'Linewidth',2); xlabel('{\it t} (sec)'); title('rectified FM derivative'); Frange=[-600 600 0 300]; figure(4) subplot(211);fd1=plot(freqs,abs(S_fm)); axis(Frange); set(fd1,'Linewidth',2); xlabel('{\it f} (Hz)'); ylabel('{\it S}_{\rm FM}({\it f})') title('FM amplitude spectrum'); subplot(212);fd2=plot(freqs,abs(S_pm)); axis(Frange); set(fd2,'Linewidth',2); xlabel('{\it f} (Hz)'); ylabel('{\it S}_{\rm PM}({\it f})') title('PM amplitude spectrum');