# How to plot this sawtooth wave and its fourier approximation

231 views (last 30 days)
Tawhid Khan on 9 Jun 2019
Edited: Utkarsh Belwal on 11 Jun 2019
I am trying to plot this wave and its fourier approximation for first 5 harmonics. This is what I want the final result to be like (note the wave approximated in the picture is a square wave) This is the wave I want to approximate: I know how to plot a sawtooth wave using the sawtooth funciton I was confused by the delay between the sawtooths in the picture. I tried using a piecewise function repeated 3 times to generate the wave.
f = @(x)[.5*x.*(0<=x & x<=2) + 0.*(2<=x & x<=3) + ((.5*x)-1.5).*(3<=x & x<=5)];
x = linspace(0,3);
intvl = [-6 6];
pfx = repmat(f(x+1),1,diff(intvl)/3);
px = linspace(intvl(1),intvl(2),length(pfx));
figure(1)
plot(px, pfx)
grid
This code can generate the sawtooth wave with some problems as shown here: Now my question is how to properly plot the function in the original question then plot a fourier transform for it. The fourier transform for this normal sawtooth below is given where L is half the length of the sawtooth.  Utkarsh Belwal on 11 Jun 2019
Edited: Utkarsh Belwal on 11 Jun 2019
freq = 1 ; % Sawtooth frequency 1Hz
T = 4 * freq ;
fs = 1000; % Sampling Rate
t = 0:1/fs:T-1/fs;
x = sawtooth(2*pi*freq*t);
plot(t,x)
acc = 0 ;
n = 3 ; % Number of harmonics.
for i = 1 : n
acc = acc + (1 / i) * sin((i * pi * t) / 0.5) ;
end
acc = 0.5 - (1 / pi)*(acc) ;
hold on
plot(t , acc)
hold off
grid on