ftest = 100000;
Npoints = 100;
x = [0:1:(Npoints-1)];
Real_transformV = 0;
Imag_transformV = 0;
Real_transformI = 0;
Imag_transformI = 0;

#voltage_wave = sin(2*pi*ftest*x/(Npoints*ftest)); #1V sine wave applied to RC circuit
voltage_wave = pi/4*square(2*pi*ftest*x/(Npoints*ftest)); #result is still close to 100n+2R when square wave excitation is applied...
voltage_wave(1) = 0;  #...but it must be...
voltage_wave(51) = 0; #...perfectly aligned in phase with the sine (so the adjustments to make it symmetric to 51)

Iamp = abs(1/(2 + 1 / (1i * 2 * pi * ftest*10^-7)));  #100nF capacitor with 2R series resistance
Iphase = arg(1/(2 + 1 / (1i * 2 * pi * ftest*10^-7)));  #100nF capacitor with 2R series resistance
current_wave = Iamp * sin(2*pi*ftest*x/(Npoints*ftest) - Iphase); #current through RC circuit

for n = 1:1:Npoints
	Real_transformV = Real_transformV + voltage_wave(n) * cos(-2 * pi * 1 * (n-1) / Npoints);
	Imag_transformV = Imag_transformV + voltage_wave(n) * sin(-2 * pi * 1 * (n-1) / Npoints);
  end;
Real_transformV
Imag_transformV

for n = 1:1:Npoints
	Real_transformI = Real_transformI + current_wave(n) * cos(-2 * pi * 1 * (n-1) / Npoints);
	Imag_transformI = Imag_transformI + current_wave(n) * sin(-2 * pi * 1 * (n-1) / Npoints);
end;
Real_transformI
Imag_transformI

#from real and imaginary parts of calculated first components of current and voltage waveforms calculate impedance
Real_Zmeas = (Real_transformV * Real_transformI + Imag_transformV * Imag_transformI) / (Real_transformI ^ 2 + Imag_transformI ^ 2);
Imag_Zmeas = (Imag_transformV * Real_transformI - Real_transformV * Imag_transformI) / (Real_transformI ^ 2 + Imag_transformI ^ 2);
C = 1 / (2 * pi * ftest * Imag_Zmeas)
R = Real_Zmeas