%
%    function Chapter6_Exc6_2.m
%      Exercise 6.2   Fourier spectrum of a rectified and AM tone (and the squared envelope)
%
%     call:     Chapter6_Exc6_2(a_M, f_M, f_C)
%
%                     JvOpstal     May 2016
%
function AN_modulation(a_M, f_M, f_C)
    if nargin<3 f_C = 1000; end
    if nargin<2 f_M = 100; end
    if nargin<1 a_M = 1.0; end
    
    dt = 1/(50*f_C);
    t = 0:dt:0.1;
    
    s = (1 + a_M * sin(2*pi*f_M .* t)).* sin(2*pi*f_C .*t);
    for n=1:length(t)
        if s(n)>0 r(n)=s(n);
           u(n)=1; 
        else r(n) = 0; 
             u(n)=0;
        end
    end
        
   s2 = s.^2;  % the squared envelope     
        
    figure(1); clf; 
    subplot(2,1,1);
    plot(t(1:end/5),s(1:end/5),'r-');
    hold on
    plot(t(1:end/5),u(1:end/5),'k-');
    xlabel('Time [s]','fontsize',13,'fontweight','bold');
    ylabel('Amplitude','fontsize',13,'fontweight','bold');
    set(gca,'fontsize',12,'fontweight','bold');
    subplot(2,1,2);
    plot(t(1:end/5),r(1:end/5),'k-');
    xlabel('Time [s]','fontsize',13,'fontweight','bold');
    ylabel('Amplitude','fontsize',13,'fontweight','bold');
    set(gca,'fontsize',12,'fontweight','bold');
    print -djpeg Exc6_2a
    
    figure(2); clf;
    Fr = fft(r, 4096);
    Gr = abs(Fr(1:2048));
    Fmax = 0.5/dt;   % 25 kHz
    freqs = 0:Fmax/2048:Fmax-Fmax/2048;
    loglog(freqs, Gr,'k-','linewidth',2);
    hold on
    axis([0 10000 5 2000])                                         % plot the spectrum to 10 kHz
    loglog(Fmax/2048, Gr(1), 'k.','markersize',20);
    xlabel('Frequency [Hz]','fontsize',13,'fontweight','bold');
    ylabel('Amplitude','fontsize',13,'fontweight','bold');
    set(gca,'fontsize',12,'fontweight','bold');
    loglog([100 100], [5 2000],'k--');
    loglog([1000 1000], [5 2000],'k--');
    loglog([900 900], [5 2000],'r--');
    loglog([1100 1100], [5 2000],'r--');
    loglog([2000 2000], [5 2000],'k--');
    loglog([1900 1900], [5 2000],'r--');
    loglog([2100 2100], [5 2000],'r--');
    print -djpeg Exc6_2c
      
    figure(3); clf;
    Fs = fft(s, 4096);
    Gs = abs(Fs(1:2048));
    loglog(freqs, Gs,'r-','linewidth',2);
    hold on
    Fu = fft(u, 4096);
    Gu = abs(Fu(1:2048));
    %loglog(freqs, Gu,'k--','linewidth',1);
    axis([0 10000 5 2000])
    loglog(Fmax/2048, Gs(1), 'r.','markersize',20);
    xlabel('Frequency [Hz]','fontsize',13,'fontweight','bold');
    ylabel('Amplitude','fontsize',13,'fontweight','bold');
    set(gca,'fontsize',12,'fontweight','bold');
    loglog([1000 1000], [5 2000],'r--');
    loglog([900 900], [5 2000],'k--');
    loglog([1100 1100], [5 2000],'k--');
    print -djpeg Exc6_2b
    
    figure(4); clf;      % the squared envelope of the signal
    Fs2 = fft(s2, 4096);
    Gs2 = abs(Fs2(1:2048));
    loglog(freqs, Gs2,'k-','linewidth',2);
    hold on
    axis([0 10000 5 2000])
    loglog(Fmax/2048, Gs2(1), 'k.','markersize',20);
    xlabel('Frequency [Hz]','fontsize',13,'fontweight','bold');
    ylabel('Amplitude','fontsize',13,'fontweight','bold');
    set(gca,'fontsize',12,'fontweight','bold');
    loglog([100 100], [5 2000],'k--');
    loglog([1000 1000], [5 2000],'k--');
    loglog([900 900], [5 2000],'r--');
    loglog([1100 1100], [5 2000],'r--');
    loglog([2000 2000], [5 2000],'k--');
    loglog([1900 1900], [5 2000],'r--');
    loglog([2100 2100], [5 2000],'r--');
    print -djpeg Exc6_2d
    
    
    return