How do I get rid of the sinusoidal wave in my output waveform for fourier series sawtooth waveform?
2 次查看(过去 30 天)
显示 更早的评论
%%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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1368424/image.png)
I was wondering if there was something wrong with my code and how to get rid of the sinusoidal wave?
0 个评论
采纳的回答
cdawg
2023-4-28
编辑:cdawg
2023-4-28
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.
更多回答(1 个)
Paul
2023-4-28
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Waveform Generation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!