% discrete_waves.m -- a MATLAB script to demonstrate the
% addition of multiple waves.
%
% Written by Michael L. Larsen for PHYS 230
% 3/9/12
% *************************************************************

% *************************************************************
% Do you want sin waves or cos waves?  Choose as follows below:
wavetype='sin';
% wavetype='cos';
% *************************************************************

numwaves = 3;
% We will use the first num_waves harmonics of the sin/cos functions.


% Random for just random waves.....
amplitudes = rand(numwaves,1);

% All in 1/n ratio for sawtooth.
% Just odds in 1/n^2 ratio for square waves.  (Leave all phases at 0).

% amplitudes = ones(numwaves,1);
% for i = 1:numwaves
%   amplitudes(i) = 1/i;
%   amplitudes(i) = 1/(i^2);
% end

%for i=1:floor(numwaves/2);
%  amplitudes(2*i) = 0;
%end

phases = 2*pi*rand(numwaves,1);
% phases = zeros(numwaves,1);

x = [0:.01:100*pi];

switch wavetype
    case 'sin'
        for i=1:numwaves
            y(:,i)=(amplitudes(i).*sin(i.*x+phases(i)));
        end
    case 'cos'
        for i=1:numwaves
            y(:,i)=(amplitudes(i).*cos(i.*x+phases(i)));
        end
end

plotting='both';
switch plotting
    case 'pieces'
        figure
        for i=1:numwaves
           subplot(numwaves,1,i)
           plot(x,y(:,i),'k')
        end
    case 'sum'
        figure
        plot(x,sum(y'),'k')
    case 'both'
        figure
        for i=1:numwaves
            subplot(numwaves,1,i)
            plot(x,y(:,i),'k')
        end
        figure
        plot(x,sum(y'),'k')
end