# fourier synthesis program when given ak

21 views (last 30 days)
Tucker Creger on 25 Sep 2016
Answered: Zhao Wang on 27 Sep 2016
I am trying to write a script for this equation:
X(Ts n) = Summation k=-N to N of Ak*e^(j*k*Wo*Ts*N)
the Ak also called C in my code is a separate matrix I have already generated for the function.
Below is the code I have:
function [ output_args ] = fsynt(C, T, Ts)
% C = a column vector of coefficients where N is the largest integer
% The sampling time = Ts
% the fundamental period = T
% Check that T/Ts is an integer
if rem(T,Ts) ~= 0; % then its an integer
disp('T/Ts not an integer');
end
% Take C and Create CC
B = conj(C(11:-1:2)); %complex conjugate
CC = horzcat(B,C) %combines negative ak and positive ak.
%Create F and multiply with CC
W0=2*pi/T; %omega
%k = -10:1:10;
F=0;
for n=0:1:2000;% n
for k = -10:1:10
F= CC*exp(1i*k*W0*Ts*n)
end
end
plot(F)
end
I am a bit lost on what to do. When I run with one period of a square wave I get a lot of matrices for F when I do not multiply it with CC in that same nested four loop, and I believe I need all of the matrices as one?
Any help is much appreciated, Thank you.

Zhao Wang on 27 Sep 2016
I understand that you want to compute Fourier synthesis using 'for' loops. The equation that you use to compute matrix 'F' creates a new matrix every time and then overwrites the previously created matrix.
To compute Fourier synthesis, you only need to use one 'for' loop. Because the 'exp' function can take vector 'n' as its input, you can first define the vector 'n' as:
>> n = 0:1:2000;
Then in the 'for' loop of variable 'k', you can use the following equation (based on what you use in the original script):
>> F = F + CC(k)*exp(1i*k*W0*Ts*n);
There is an example that shows how to approximate a square wave through Fourier synthesis. Refer to the answer provided by Youssef Khmou in the following link: