%This script generates the partial sum of a sawtooth wave
%by summing its harmonics.
%Plot of the first few harmonics are in the Sawtooth
%picture folder.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Sawtooth Wave Synthesis %
% Author : Reza Ghassemi %
% Date : 14 June 2005 %
% email : rghassemi@gmail.com %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%This file constructs the partial sum of a sawtooth wave.
%
%f0 is the fundamental frequency.
%Fs is the sampling rate
%length is the length of the signal
%N is the number of waveforms used to construct the signal.
%x is the output time-domain vector.
%Xf is the output frequency-domain vector
%The function returns a time-domain plot of the partial sum.
% It also returns a frequency-domain plot of the partial sum.
function [x,Xf]=partialsaw(f0,Fs,length,N)
t=1:length;
x=zeros(1,length);
for i=1:N
x=x+(1/i)*sin(2*pi*(i*f0/Fs)*t);
end
subplot(211);
plot(t,x);
xlabel('time');
title('Time Domain');
Xf=abs(fft(x));
subplot(212);
f=linspace(-Fs/2,Fs/2,length);
stem(f,fftshift(Xf));
xlabel('Hertz');
title('Frequency Domain');