function [x,t,Dn,w]=ctfs_synthesis(T0,N,Dnfxn,t,arg1,arg2,arg3) %%% Function to implement CT Fourier series synthesis equation %%% %%% [x,t]=ctfs_synthesis(T0,N,Dnfxn,t,arg1,arg2,arg3); %%% %%% Variables: %%% T0 = fundamental period %%% N = number of Fourier series coefficients to use %%% Dnfxn = string containing name of function to compute FS %%% coefficients. This function takes one argument: the n %%% vector. %%% t = vector of times [OPTIONAL; if no times are specified %%% function computes values for 2 periods] %%% arg1 = OPTIONAL first argument to send to the function specified %%% by Dnfxn %%% arg2 = OPTIONAL second argument to send to the function specified %%% by Dnfxn %%% arg3 = OPTIONAL third argument to send to the function specified %%% by Dnfxn %%% x = x(t) %%% if nargin<3 error('CTFS_SYNTHESIS: not enough input arguments') elseif nargin<4 | isempty(t) if N>0 maxfreq=N/T0; else maxfreq=1/T0; end t=-T0:1/(8*maxfreq):T0; % if t not specified, sample at four times Nyquist end w0=2*pi/T0; % Fundamental frequency n=-N:N; % Vector of n's En=exp(j*t'*n*w0); % Matrix of complex exponentials %%% Call the function to generate the Fourier series coefficients if nargin==5 Dn=feval(Dnfxn,n,arg1); % 1 optional argument specified elseif nargin==6 Dn=feval(Dnfxn,n,arg1,arg2); % 2 optional arguments specified elseif nargin==7 Dn=feval(Dnfxn,n,arg1,arg2,arg3); % 3 optional arguments specified else Dn=feval(Dnfxn,n); % No optional arguments specified end Dn=Dn(:); % Make sure Dn is column vector w=n*w0; x=En*Dn; % Compute x as matrix multiply %%% Note: 100*eps is on order of 2*10^(-14). This is needed to deal %%% with impulse train calculations. You'll still get imaginary values %%% if you use lots of coefficients for the impulse train. if max(abs(imag(x)))<100*eps % Make sure x is real x=real(x); % (eliminate small imaginary part) end return