
A = 0.5;                    % signal amplitude
f = [800 1600 3200 6800].'; % frequencies to compute
fs = 10000;                 % sample rate 
duration = 2;               % duration in seconds

period = 1/fs;
t = 0:period:duration;      % time vector

t_min_display = 0;
t_max_display = 3 / min(f); % plot 3 periods of the lowest-frequency signal   

   
figure;                 % illustrate
for i = 1:length(f)
    
    y = A*sin(2*pi*f(i)*t);  % "sample" signal
   
    subplot(length(f),1,i);
    plot(t,y,'o-');
    xlim([t_min_display,t_max_display]);
    xlabel('time (seconds)');
    ylabel([num2str(f(i)) ' Hz']);
    
    sound(y,fs);
    disp('paused ... hit key to continue')
    pause;
end


% illustrate the aliasing effect

figure;
for i = 1:length(f)
    subplot(length(f),1,i);
    t_0 = 0:1/(20*fs):duration;
    y_0 = A*sin(2*pi*f(i)*t_0);  
    plot(t_0,y_0,'k--','LineWidth',1);
    hold on;
    
    y = A*sin(2*pi*f(i)*t);  
   
    plot(t,y,'bo-','LineWidth',1);
    xlim([t_min_display,t_max_display]);
    xlabel('t');
    ylabel([num2str(f(i)) ' Hz']);

end