24 #define DEF_FOURGRIDSIZE 200 34 struct dvec *time, *vec;
37 double *ff, fundfreq, *dp, *stuff;
38 int nfreqs, fourgridsize, polydegree;
39 double *freq, *mag, *phase, *nmag, *nphase;
40 double thd, *timescale, *grid,
d;
47 sprintf(xbuf,
"%1.1e", 0.0);
48 shift = strlen(xbuf) - 7;
50 fprintf(
cp_err,
"Error: no vectors loaded.\n");
65 fprintf(
cp_err,
"Error: fourier needs real time scale\n");
69 if (!(ff =
ft_numparse(&s,
false)) || (*ff <= 0.0)) {
70 fprintf(
cp_err,
"Error: bad fund freq %s\n", wl->wl_word);
87 if (dp[1] - dp[0] < d) {
89 "Error: wavelength longer than time span\n");
92 else if (dp[1] - dp[0] > d) {
97 freq = (
double *)
tmalloc(nfreqs *
sizeof (
double));
98 mag = (
double *)
tmalloc(nfreqs *
sizeof (
double));
99 phase = (
double *)
tmalloc(nfreqs *
sizeof (
double));
100 nmag = (
double *)
tmalloc(nfreqs *
sizeof (
double));
101 nphase = (
double *)
tmalloc(nfreqs *
sizeof (
double));
103 grid = (
double *)
tmalloc(fourgridsize *
sizeof (
double));
104 stuff = (
double *)
tmalloc(fourgridsize *
sizeof (
double));
106 d = (dp[1] - dp[0]) / fourgridsize;
107 for (i = 0; i < fourgridsize; i++)
108 grid[i] = dp[0] + i * d;
115 for (dl = dl0; dl; dl = dl->
dl_next) {
119 "Error: lengths don't match: %d, %d\n",
124 fprintf(
cp_err,
"Error: %s isn't real!\n",
134 grid, fourgridsize, polydegree)) {
136 "Error: can't interpolate\n");
145 err =
CKTfour(fourgridsize, nfreqs, &thd, timescale,
146 stuff, fundfreq, freq, mag, phase, nmag, nphase);
154 " No. Harmonics: %d, THD: %g %%, Gridsize: %d, Interpolation Degree: %d\n\n",
155 nfreqs, thd, fourgridsize, polydegree);
160 out_printf(
"Harmonic %-*s %-*s %-*s %-*s %-*s\n",
161 fw,
"Frequency", fw,
"Magnitude",
162 fw,
"Phase", fw,
"Norm. Mag",
164 out_printf(
"-------- %-*s %-*s %-*s %-*s %-*s\n",
165 fw,
"---------", fw,
"---------",
166 fw,
"-----", fw,
"---------",
168 for (i = 0; i < nfreqs; i++) {
203 CKTfour(ndata,numFreq,thd,Time,Value,FundFreq,Freq,Mag,Phase,nMag,nPhase)
234 for(i=0;i<numFreq;i++) {
238 for(i=0;i<ndata;i++) {
239 for(j=0;j<numFreq;j++) {
240 Mag[j] += (Value[i]*
sin(j*2.0*
M_PI*i/((
double) ndata)));
241 Phase[j] += (Value[i]*
cos(j*2.0*
M_PI*i/((
double) ndata)));
245 Mag[0] = Phase[0]/ndata;
246 Phase[0]=nMag[0]=nPhase[0]=Freq[0]=0;
248 for(i=1;i<numFreq;i++) {
249 tmp = Mag[i]*2.0 /ndata;
250 Phase[i] *= 2.0/ndata;
251 Freq[i] = i * FundFreq;
252 Mag[i] = sqrt(tmp*tmp+Phase[i]*Phase[i]);
253 Phase[i] = atan2(Phase[i],tmp)*180.0/
M_PI;
254 nMag[i] = Mag[i]/Mag[1];
255 nPhase[i] = Phase[i]-Phase[1];
256 if(i>1) *thd += nMag[i]*nMag[i];
258 *thd = 100*sqrt(*thd);
bool cp_getvar(char *n, int t, char *r)
struct dvlist * ft_dvlist()
struct pnode * ft_getpnames()
void com_fourier(wordlist *wl)