Hello,
I have a question regarding DFT/Fast Fourier Transformation and signal processing.
I want to write a program (matlab in this case) which can calculate the reaction (current) on an arbitrary waveform (voltage).
My idea was to take the arbitrary waveform, do FFT, devide the FFT coefficients with the impedance at the desired frequency: I(f)=U(f)/Z(f), and then do an inverse FFT I(f)->I(t).
This is working fine for harmonic waveforms. But when I try to do this with a square wave it fails at the edges.
The attached picture shows U(t), I(t) calculated with matlab and the same thing with ltspice. The rising current at the edge seems to be wrong comparing the result with ltspice.
Thank you in advance for every hint and solution.
matlab code:
======================
clear all;
clc;
%define measurement
Fs = 10000; % Sampling frequency
T = 1/Fs; % Sampling period
L =Fs; % Length of signal
t = T*(0:L-1); % Time vector
f = Fs*(1:L-1)/L;
%define input waveform U(t)
U = 4*(1+square(10*t))/2;
%U0=2;
%U = U0*exp(i*2*pi*300*t)+U0*exp(i*2*pi*100*t)
%do the fft of U(t)->U(f)
Ufft = fft(U);
%plot the FFT of U(t)
fig_4=figure(4);
ax_4=axes;
semilogy(abs(Ufft));
%plot U(t)
fig_1=figure(1);
ax_1=axes;
plot(ax_1,t,real(U))
title('Input U(t)')
fig_1.Position = [0,343,560,420];
%define a frequency vector with rising and falling frequencies looking like
%this: /\ because of the mirror coefficients.
freq=Fs*(0:L/2-1)/L;
freq=horzcat(freq,fliplr(freq));
%define the impedance (here only C and R in series)
L=0;
Cap=100e-6;
R=1;
res=1/(2*pi*sqrt(L*Cap)) %calculate resonance frequency
Xl=i*2*pi*L.*freq;
Xc=-i./(2*pi*Cap*freq);
Z = R+Xl+Xc; %calculate impedance of the system
fig_2=figure(2);
ax_2=axes;
semilogy(freq,abs(Z))
title('Impedance Spectrum of Z(f)')
fig_2.Position = [560,343,560,420];
Ifft=Ufft./Z; %devide the Fourier coefficients by the impedance Z(f)
fig_3=figure(3);
ax_3=axes;
current=ifft(Ifft);
plot(ax_3,t,real(current))
fig_3.Position = [1120,343,560,420];
title('Output I(t)')
max(current)
======================