Hello all, Recently, I simulated a Track and hold...
# general
a
Hello all, Recently, I simulated a Track and hold circuit in Xschem+NGspice and exported the simulation raw file for post processing fft. The signal in time domain seems to be perfect but in frequency domain it looks like something is wrong, if anyone can help, I'd be very thankful. attached the circuit + post processing script + output wave form in time and frequency domains.
Copy code
clear all;
close all;
clc;
path = "D:\studies\SHA.raw";
[signals, signals_values] = parse_tran_ngspice(path);
time = signals_values(find(strcmp(signals, "time")),:);
y = signals_values(find(strcmp(signals, "v(hold)")),:);
plot(time,y);
N = length(y);
s = abs(fft(y,N))
%s=s(1:N/2);
f= (0:length(s)-1)/N;
sdb = 20*log10(s);
plot(f, sdb);
xlabel('Frequency [ f / f_s ]');
ylabel('Amplitude [ dBFS ]');
m
I'd suspect
plot(y)
. Transient has no constant timestep and
fft(y,N)
doesn't know about the
time
vector
a
What do you suggest?
Should I get N from the time vector? I tried.
Os is this unsolvable?
m
Check if
time
does not have constant timesteps, if confirmed rebase y with interp1()
a
It actually doesn’t have constant time-steps, how can I rebase y?
a
Thanks.
@Manuel M Is there a way of implementing a constant time step in the transient settings in the circuit simulation?
I’m not in complete understanding of why it’s not constant.
k
@Aly you can run ngspice terminal mode and write the following: set lin-tstep=10n linearize v(VSH) wrdata txt v(VSH) then you will have a linearized version from your wave in the simulation files. However, something is still missing!
a
Thanks, Karim!
s
in ngspice there is a
linearize
command that transforms tran vectors to constant timestep: section in ngspice manual: 17.5.40 Linea`rize*: Interpolate to a linear scale.` This should be done before processing data for fft.
a
Are the voltage values captured at the constant time steps times or at the non linear then linearised by a math function? Thanks, Stefan.
Because after including that command, the time vector became linear but the spectrum is still suspicious.
s
As far as I understand the Linearize command will sample the vector to the first interval given in the
tran
command. Example: in my simulation I have a vector generated with a
tran 0.05u 1m
that is
25852
points long. Applying the linearize command i get a
20001
points vector, which is exactly a vector running from 0 to 1m with 50ns increments. Voltage values are interpolated if no exact X-value is present (I guess taking the point before and the point following and doing linear interpolation, but I have not checked that).
a
I understand, do you think that if the process is interpolating the values if no
y
values exist for the `x`(linearised) values will affect the spectrum assuming that the input signal is a sine wave. I wouldn’t say that it’ll be affected if the input is a ramp. Do you agree?
s
I agree. Verify that the sampling time interval is dense enough to cover the highest signal frequency. If not restart the tran command with a smaller interval.
a
Will try that! Do you also think that rectangular windowing causing discontinuity between periods of the fft and causing spectral leakage? Thanks so much, Stefan!
s
I think for this question you are much more expert than me !
And if in doubt you can tune your tran command to have start and end point with no discontinuity. You can also save only after an initial setup with the tran command:
tran tstep tstop tstart tmax
tstart
tells where to start saving in the raw file. (simulation will always start from zero but no data is stored).
tstop
is the end point
tmax
limits the maximum time step.
a
That was very helpful, thanks.