help with matlab code!

3 vues (au cours des 30 derniers jours)
Chaileen Carredo
Chaileen Carredo le 3 Mai 2023
The code below is what im using and i am trying to have y2 match up to the original square waveform, im not too sure why y2 isnt showing up
%% Square Waveform Fourier Series Representation
%%Please do not change the following code%%
fs = 44100; % Sampling frequency
Ts = 1/fs; % Time step
t1 = 0 : Ts : 2 - Ts; % 0 - 2s with time step Ts
x1 = square((1000*pi*t1) + pi/2); % Original Square Waveform
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Enter the DC constant
T0 = 1; f0 = 1/T0;
y1 = 0;
for i = 1:8;
% Use your Fourier Series calculation result in the following
y1 = y1+sin(2*pi*t1(2*i-1))/(2*i-1);
end
% Plot the time domain figure x1 and y1 in the following
figure(1);
plot(t1, x1, t1, y1);
xlim([0 6e-3]);

Réponses (1)

chicken vector
chicken vector le 3 Mai 2023
Modifié(e) : chicken vector le 3 Mai 2023
fs = 44100; % Sampling frequency
Ts = 1/fs; % Time step
t1 = 0 : 2*pi*Ts : 2*pi;
x1 = square(t1);
y1 = 0;
for i = 1 : 8
% Use your Fourier Series calculation result in the following
y1 = y1+sin(t1*(2*i-1))/(2*i-1);
end
% Plot the time domain figure x1 and y1 in the following
figure;
hold on;
plot(t1, x1,'LineWidth',2);
plot(t1, y1,'.-','LineWidth',2);
hold off;
You can scale the time axis by dividing t1 and you can equalise the square waves with a factor of 4/5:
period = 5e-3;
t1 = t1*period/(2*pi);
y1 = 5*y1/4;
figure;
hold on;
plot(t1, x1,'LineWidth',2);
plot(t1, y1,'.-','LineWidth',2);
hold off;
xlabel('Time [s]');
ylabel('Current [A]');

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by