1FFT_Leakage_tests http://www.idea2ic.com/
2*=========Create_Signal==================
3VTime     VTime  0      DC     0      PWL(   0      0    1      1)
4Vfreq     Vfreq  0      DC     5.5k
5BVAC      IN     0      V  =   sin( 6.283185307179586*V(VFreq)*V(VTime))
6.control
7*TRAN     TSTEP  TSTOP  TSTART TMAX   ?UIC?
8tran      1u     .999m  0       1u
9set       pensize = 2
10linearize
11let       numb2 = length(in)
12print     numb2
13
14*=========Do_FFT_and_Plot_As_dB_Freq==================
15let       ac = in +j(0)
16let       ac_fft=fft(ac)
17let       numb_f2 =  (numb2)/2 -1
18compose   freq   start = 1 stop = $&numb_f2  step =1
19compose   vreal  start = 1 stop = $&numb_f2  step =1
20compose   vimag  start = 1 stop = $&numb_f2  step =1
21let       j = 0
22repeat    $&numb_f2
23let       freq[j] =  freq[j]
24let       vreal[j] = 2*real(ac_fft[j+1])
25let       vimag[j] = 2*imag(ac_fft[j+1])
26let       j = j +1
27end
28plot      dB(abs(vreal+1f)) dB(abs(vimag+1f)) vs freq xlog
29
30*=========Extract_Error_Signal=========================
31let       funBin                 = 5k/1000
32let       unvect                 = unitvec(numb2)
33let       fundspec               = unvect*0 +j(0)
34let       fundspec[funBin]       = real(ac_fft[funBin])        +j(imag(ac_fft[funBin] ))
35let       fundspec[numb2-funBin] = real(ac_fft[numb2-funBin])  +j(imag(ac_fft[numb2-funBin] ))
36let       fund                   = ifft(fundspec)
37let       dc_ofset               = real(ac_fft[0])
38let       thdspec                = ac_fft
39let       thdspec[0]             = 0        +j(0)
40let       thdspec[funBin]        = 0        +j(0)
41let       thdspec[numb2-funBin]  = 0        +j(0)
42let       thd                    = ifft(thdspec)
43plot      norm(in) norm(fund) norm(thd)/2
44
45*=========Calc_Values=========================
46let       rms_Fund               = sqrt(mean(fund*fund))
47let       rms_THD                = sqrt(mean(thd*thd))
48let       THD_percent            = 100*rms_THD/rms_Fund
49let       FREQ_Hz                = VFreq[0]
50echo      "Freq_Hz=$&FREQ_Hz THD_percent=$&THD_percent Fund_rms=$&rms_Fund THD_rms=$&rms_THD "
51
52.endc
53.end
54