% demo of aliasing Fs = 1 ; Ts = 1/Fs ; T = 5 ; % display 5 seconds t = 0:Ts:T ; % sample times tover = 0:0.005:T ; % time scale for oversampling (analog signal) f = 0.417 * Fs ; % start somewhere % set up graphics close all scrsz = get(0,'ScreenSize') ; % top figure is for selecting frequency figure('OuterPosition',[1 3*scrsz(4)/4 scrsz(3) scrsz(4)/4]) ; % figure 1 rectangle('Position', [0 0 0.5 1], 'FaceColor', [0.9 0.9 0.9]) % nonaliased area rectangle('Position', [-0.5 0 0.5 1], 'FaceColor', [0.95 0.95 0.95]) % negative nonaliased area axis([-T T 0 1]) ; set(gca, 'GridAlpha', 1) grid on set(gca,'YTickLabel',[]) hold on % bottom figure is for displaying signals figure('OuterPosition',[1 1 scrsz(3) 3*scrsz(4)/4]) ; % figure 2 axis([0 T -1 1]); hold on % loop on sine frequencies while f > 0 figure(1) [f,~] = ginput(1) ; % input desired frequency if f<0 return % stop end w = 2*pi*f ; cla % erase previous rectangle('Position', [0 0 0.5 1], 'FaceColor', [0.9 0.9 0.9]) % nonaliased area rectangle('Position', [-0.5 0 0.5 1], 'FaceColor', [0.95 0.95 0.95]) % negative nonaliased area grid on scatter(f, 0.5, 200, 'b', 'filled') s = sin(w*t) ; sover = sin(w*tover); % display analog and sampled signal figure(2) cla % erase previous plot(t,s,'or', 'MarkerFaceColor','r', 'MarkerSize',15); % plot sample values plot(tover,sover,'b') % plot oversampled sine (analog signal) % now show aliased sinusoid (if there is one) if f > 0.5 % over Nyquist frequency hold on falias = f - Fs ; while falias > 0.5*Fs falias = falias - Fs ; end walias = 2*pi*falias ; salias = sin(walias*tover); figure(1) scatter(falias, 0.5, 200, 'g', 'filled') % show aliased frequency figure(2) hold on plot(tover,salias, 'g') % plot alised sine end end