How do I get rid of the sinusoidal wave in my output waveform for fourier series sawtooth waveform?
4 views (last 30 days)
Show older comments
Chaileen Carredo
on 28 Apr 2023
Commented: Chaileen Carredo
on 30 Apr 2023
%%Please do not change the following code%%
fs = 44100; % Sampling frequency
Ts = 1/fs; % Time step.
t2 = -3 * pi : Ts : 3 * pi; % -3pi - 3pi s with time step Ts
% Original Sawtooth Waveform
x2 = (t2 + 2 * pi).*(t2 < -pi) + t2.*((-pi <= t2) & (t2 <= pi)) + (t2 - 2 * pi).*(t2 > pi);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
T0 = 2*pi; f0 = 1/T0;
y2 = 0;
for i = 1: 8
y2 = y2 + sin(k*f0*t2)/k;
end
figure(2);
plot(t2, x2, t2, y2);
This is my code for the sawtooth graph however it outputs this
I was wondering if there was something wrong with my code and how to get rid of the sinusoidal wave?
0 Comments
Accepted Answer
cdawg
on 28 Apr 2023
Edited: cdawg
on 28 Apr 2023
I'm not totally sure by what you mean. If you mean get rid of the sine wave meaning just don't plot it:
fs = 44100; % Sampling frequency
Ts = 1/fs; % Time step.
t2 = -3 * pi : Ts : 3 * pi; % -3pi - 3pi s with time step Ts
% Original Sawtooth Waveform
x2 = (t2 + 2 * pi).*(t2 < -pi) + t2.*((-pi <= t2) & (t2 <= pi)) + (t2 - 2 * pi).*(t2 > pi);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
figure(2);
plot(t2, x2);
ylim([-4 4])
Otherwise, it looks like the sine wave you're talking about is using a Fourier series to construct a sawtooth wave using additive synthesis (ref). I made some slight modifications to this part of your script to follow the equation I linked in my reference. Synthesizing the sawtooth wave 8 times (k=8), we get:
fs = 44100; % Sampling frequency
Ts = 1/fs; % Time step.
t2 = -3 * pi : Ts : 3 * pi; % -3pi - 3pi s with time step Ts
% Original Sawtooth Waveform
x2 = (t2 + 2 * pi).*(t2 < -pi) + t2.*((-pi <= t2) & (t2 <= pi)) + (t2 - 2 * pi).*(t2 > pi);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
T0 = 2*pi;
f0 = 1/T0;
y2 = 0;
a = 2*pi;
for k = 1: 8
y2 = y2 + ((-1)^k)*sin(2*pi*k*f0*t2)/k;
end
y2 = a*(0.5-(1/pi)*y2);
figure();
plot(t2, x2, t2, y2);
Let's try k = 80, see that the more iterations you use the closer you get to the original sawtooth waveform:
fs = 44100; % Sampling frequency
Ts = 1/fs; % Time step.
t2 = -3 * pi : Ts : 3 * pi; % -3pi - 3pi s with time step Ts
% Original Sawtooth Waveform
x2 = (t2 + 2 * pi).*(t2 < -pi) + t2.*((-pi <= t2) & (t2 <= pi)) + (t2 - 2 * pi).*(t2 > pi);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
T0 = 2*pi;
f0 = 1/T0;
y2 = 0;
a = 2*pi;
for k = 1: 80
y2 = y2 + ((-1)^k)*sin(2*pi*k*f0*t2)/k;
end
y2 = a*(0.5-(1/pi)*y2);
figure();
plot(t2, x2, t2, y2);
For some reason my amplitude is shifted by pi. Not totally sure why but the shape itself seems correct.
More Answers (1)
Paul
on 28 Apr 2023
Hi Chaileen,
It looks like you're trying to use a summation like this:
for n = 1:8
y2 = y2 + B(n)*sin(2*pi*n*t2/(T0))
end
The code doesn't show the value of k, but even with k = 2*pi the argument to the sin() is still missing that factor of n.
Also, the code is assuming that B(n) = 1/k, which isn't correct. Suggest revisiting the derivation of that Fourier series coefficient, or rechecking the code against its equation if the equation was given to you.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!