ngspice/src/frontend/fourier.c

277 lines
9.1 KiB
C
Raw Normal View History

2000-04-27 22:03:57 +02:00
/**********
Copyright 1990 Regents of the University of California. All rights reserved.
Author: 1985 Wayne A. Christopher, U. C. Berkeley CAD Group
**********/
/*
* Code to do fourier transforms on data. Note that we do interpolation
* to get a uniform grid. Note that if polydegree is 0 then no interpolation
* is done.
*/
#include "ngspice.h"
#include "cpdefs.h"
#include "ftedefs.h"
* src/Makefile.am src/main.c src/sconvert.c src/analysis/cktdisto.c src/analysis/cktnoise.c src/analysis/noisean.c: Updates for the new header files. * src/maths/cmaths/cmath1.c src/maths/cmaths/cmath2.c src/maths/cmaths/cmath3.c src/maths/cmaths/cmath4.c: Updates for the new header files. * src/frontend/.cvsignore src/frontend/Makefile.am: Updates for the new files. * src/frontend/agraf.c src/frontend/aspice.c src/frontend/breakp.c src/frontend/breakp2.c src/frontend/circuits.c src/frontend/cpitf.c src/frontend/debugcom.c src/frontend/define.c src/frontend/diff.c src/frontend/dimens.c src/frontend/display.c src/frontend/doplot.c src/frontend/dotcards.c src/frontend/evaluate.c src/frontend/fourier.c src/frontend/graf.c src/frontend/grid.c src/frontend/inp.c src/frontend/inpcom.c src/frontend/interp.c src/frontend/linear.c src/frontend/misccoms.c src/frontend/misccoms.h src/frontend/miscvars.c src/frontend/mw_coms.c src/frontend/newcoms.c src/frontend/nutinp.c src/frontend/options.c src/frontend/outitf.c src/frontend/parse.c src/frontend/plotcurv.c src/frontend/points.c src/frontend/postcoms.c src/frontend/rawfile.c src/frontend/runcoms.c src/frontend/runcoms2.c src/frontend/shyu.c src/frontend/spec.c src/frontend/spiceif.c src/frontend/typesdef.c src/frontend/vectors.c src/frontend/where.c src/frontend/postcoms.c: Updates for the new header files. Some commands have moved into the new files below. * src/frontend/README src/frontend/com_compose.c src/frontend/com_compose.h src/frontend/com_display.c src/frontend/com_display.h src/frontend/com_let.c src/frontend/com_let.h src/frontend/com_setscale.c src/frontend/com_setscale.h src/frontend/commands.c src/frontend/commands.h src/frontend/completion.h src/frontend/streams.h src/frontend/testcommands.c: Separation into different com_* commands. This is a start. The rest of the subdirectory needs doing. * src/include/complex.h src/include/cpdefs.h src/include/cpextern.h src/include/cpstd.h src/include/fteconst.h src/include/ftedata.h src/include/ftedev.h src/include/fteext.h src/include/ftegraph.h src/include/fteparse.h src/include/dvec.h src/include/grid.h src/include/plot.h src/include/pnode.h src/include/sim.h src/include/variable.h src/include/wordlist.h src/include/bool.h: Separation of header files into smaller pieces. This limits recompilation to only the affected source files. The original header files have a warning message embedded to flag obsoleted use. * src/frontend/compose.c src/frontend/compose.h src/frontend/nutctab.c src/frontend/nutctab.h src/frontend/plot5.c src/frontend/plot5.h src/frontend/spcmdtab.c src/frontend/x11.c src/frontend/x11.h src/frontend/xgraph.c src/frontend/xgraph.h: Moved these files into src/frontend/plotting subdirectory. * src/frontend/plotting/.cvsignore src/frontend/plotting/Makefile.am src/frontend/plotting/plot5.c src/frontend/plotting/plot5.h src/frontend/plotting/plotting.c src/frontend/plotting/plotting.h src/frontend/plotting/pvec.c src/frontend/plotting/pvec.h src/frontend/plotting/x11.c src/frontend/plotting/x11.h src/frontend/plotting/xgraph.c src/frontend/plotting/xgraph.h: The new libplotting library with automake and CVS infrastructure.
2000-05-06 16:12:51 +02:00
#include "dvec.h"
2000-04-27 22:03:57 +02:00
#include "fteparse.h"
#include "sperror.h"
#include "const.h"
src/Makefile.am src/help.c src/main.c src/circuit/Makefile.am src/circuit/ifnewuid.c src/frontend/Makefile.am src/frontend/aspice.c src/frontend/circuits.h src/frontend/com_display.c src/frontend/com_hardcopy.c src/frontend/commands.c src/frontend/commands.h src/frontend/cpitf.c src/frontend/debugcom.c src/frontend/device.c src/frontend/diff.c src/frontend/display.c src/frontend/dotcards.c src/frontend/fourier.c src/frontend/inp.c src/frontend/inpcom.c src/frontend/linear.c src/frontend/misccoms.c src/frontend/mw_coms.c src/frontend/nutinp.c src/frontend/options.c src/frontend/outitf.c src/frontend/parse.c src/frontend/postcoms.c src/frontend/postsc.c src/frontend/rawfile.c src/frontend/resource.c src/frontend/runcoms.c src/frontend/runcoms2.c src/frontend/shyu.c src/frontend/spec.c src/frontend/spiceif.c src/frontend/subckt.c src/frontend/vectors.c src/frontend/where.c src/frontend/plotting/Makefile.am src/frontend/plotting/agraf.c src/frontend/plotting/graf.c src/frontend/plotting/plotcurv.c src/frontend/plotting/plotit.c src/frontend/plotting/x11.c src/frontend/plotting/xgraph.c src/include/Makefile.am src/maths/cmaths/cmath4.c src/misc/terminal.c src/misc/terminal.h src/parser/cshpar.c src/parser/front.c src/parser/front.h src/parser/history.c src/parser/history.h src/parser/modify.c src/parser/var2.c src/parser/var2.h src/parser/variable.c: Refactoring of frontend code. * src/include/ftehelp.h src/include/variable.h: Moved into frontend directory. * src/include/cpdefs.h src/include/cpextern.h src/include/ftedefs.h src/include/plot.h: Updates.
2000-06-27 18:09:02 +02:00
2000-04-27 22:03:57 +02:00
#include "fourier.h"
src/Makefile.am src/help.c src/main.c src/circuit/Makefile.am src/circuit/ifnewuid.c src/frontend/Makefile.am src/frontend/aspice.c src/frontend/circuits.h src/frontend/com_display.c src/frontend/com_hardcopy.c src/frontend/commands.c src/frontend/commands.h src/frontend/cpitf.c src/frontend/debugcom.c src/frontend/device.c src/frontend/diff.c src/frontend/display.c src/frontend/dotcards.c src/frontend/fourier.c src/frontend/inp.c src/frontend/inpcom.c src/frontend/linear.c src/frontend/misccoms.c src/frontend/mw_coms.c src/frontend/nutinp.c src/frontend/options.c src/frontend/outitf.c src/frontend/parse.c src/frontend/postcoms.c src/frontend/postsc.c src/frontend/rawfile.c src/frontend/resource.c src/frontend/runcoms.c src/frontend/runcoms2.c src/frontend/shyu.c src/frontend/spec.c src/frontend/spiceif.c src/frontend/subckt.c src/frontend/vectors.c src/frontend/where.c src/frontend/plotting/Makefile.am src/frontend/plotting/agraf.c src/frontend/plotting/graf.c src/frontend/plotting/plotcurv.c src/frontend/plotting/plotit.c src/frontend/plotting/x11.c src/frontend/plotting/xgraph.c src/include/Makefile.am src/maths/cmaths/cmath4.c src/misc/terminal.c src/misc/terminal.h src/parser/cshpar.c src/parser/front.c src/parser/front.h src/parser/history.c src/parser/history.h src/parser/modify.c src/parser/var2.c src/parser/var2.h src/parser/variable.c: Refactoring of frontend code. * src/include/ftehelp.h src/include/variable.h: Moved into frontend directory. * src/include/cpdefs.h src/include/cpextern.h src/include/ftedefs.h src/include/plot.h: Updates.
2000-06-27 18:09:02 +02:00
#include "variable.h"
2000-04-27 22:03:57 +02:00
/* static declarations */
static char * pn(double num);
static int CKTfour(int ndata, int numFreq, double *thd, double *Time, double *Value,
double FundFreq, double *Freq, double *Mag, double *Phase, double *nMag,
double *nPhase);
#define DEF_FOURGRIDSIZE 200
/* CKTfour(ndata,numFreq,thd,Time,Value,FundFreq,Freq,Mag,Phase,nMag,nPhase)
* len 10 ? inp inp inp out out out out out
*/
void
com_fourier(wordlist *wl)
{
struct dvec *time, *vec;
struct pnode *names, *first_name;
double *ff, fundfreq, *dp, *stuff;
int nfreqs, fourgridsize, polydegree;
double *freq, *mag, *phase, *nmag, *nphase; /* Outputs from CKTfour */
double thd, *timescale, *grid, d;
char *s;
int i, err, fw;
char xbuf[20];
int shift;
sprintf(xbuf, "%1.1e", 0.0);
shift = strlen(xbuf) - 7;
if (!plot_cur || !plot_cur->pl_scale) {
fprintf(cp_err, "Error: no vectors loaded.\n");
return;
}
if ((!cp_getvar("nfreqs", VT_NUM, (char *) &nfreqs)) || (nfreqs < 1))
nfreqs = 10;
if ((!cp_getvar("polydegree", VT_NUM, (char *) &polydegree)) ||
(polydegree < 0))
polydegree = 1;
if ((!cp_getvar("fourgridsize", VT_NUM, (char *) &fourgridsize)) ||
(fourgridsize < 1))
fourgridsize = DEF_FOURGRIDSIZE;
time = plot_cur->pl_scale;
if (!isreal(time)) {
fprintf(cp_err, "Error: fourier needs real time scale\n");
return;
}
s = wl->wl_word;
if (!(ff = ft_numparse(&s, FALSE)) || (*ff <= 0.0)) {
fprintf(cp_err, "Error: bad fund freq %s\n", wl->wl_word);
return;
}
fundfreq = *ff;
freq = (double *) tmalloc(nfreqs * sizeof (double));
mag = (double *) tmalloc(nfreqs * sizeof (double));
phase = (double *) tmalloc(nfreqs * sizeof (double));
nmag = (double *) tmalloc(nfreqs * sizeof (double));
nphase = (double *) tmalloc(nfreqs * sizeof (double));
wl = wl->wl_next;
names = ft_getpnames(wl, TRUE);
first_name = names;
while (names) {
vec = ft_evaluate(names);
names = names->pn_next;
while (vec) {
if (vec->v_length != time->v_length) {
fprintf(cp_err,
"Error: lengths don't match: %d, %d\n",
vec->v_length, time->v_length);
continue;
}
if (!isreal(vec)) {
fprintf(cp_err, "Error: %s isn't real!\n",
vec->v_name);
continue;
}
if (polydegree) {
/* Build the grid... */
grid = (double *) tmalloc(fourgridsize *
sizeof (double));
stuff = (double *) tmalloc(fourgridsize *
sizeof (double));
dp = ft_minmax(time, TRUE);
/* Now get the last fund freq... */
d = 1 / fundfreq; /* The wavelength... */
if (dp[1] - dp[0] < d) {
fprintf(cp_err,
"Error: wavelength longer than time span\n");
return;
} else if (dp[1] - dp[0] > d) {
dp[0] = dp[1] - d;
}
d = (dp[1] - dp[0]) / fourgridsize;
for (i = 0; i < fourgridsize; i++)
grid[i] = dp[0] + i * d;
/* Now interpolate the stuff... */
if (!ft_interpolate(vec->v_realdata, stuff,
time->v_realdata, vec->v_length,
grid, fourgridsize,
polydegree)) {
fprintf(cp_err,
"Error: can't interpolate\n");
return;
}
timescale = grid;
} else {
fourgridsize = vec->v_length;
stuff = vec->v_realdata;
timescale = time->v_realdata;
}
err = CKTfour(fourgridsize, nfreqs, &thd, timescale,
stuff, fundfreq, freq, mag, phase, nmag,
nphase);
if (err != OK) {
ft_sperror(err, "fourier");
return;
}
fprintf(cp_out, "Fourier analysis for %s:\n",
vec->v_name);
fprintf(cp_out,
" No. Harmonics: %d, THD: %g %%, Gridsize: %d, Interpolation Degree: %d\n\n",
nfreqs, thd, fourgridsize,
polydegree);
/* Each field will have width cp_numdgt + 6 (or 7
* with HP-UX) + 1 if there is a - sign.
*/
fw = ((cp_numdgt > 0) ? cp_numdgt : 6) + 5 + shift;
fprintf(cp_out, "Harmonic %-*s %-*s %-*s %-*s %-*s\n",
fw, "Frequency", fw, "Magnitude",
fw, "Phase", fw, "Norm. Mag",
fw, "Norm. Phase");
fprintf(cp_out, "-------- %-*s %-*s %-*s %-*s %-*s\n",
fw, "---------", fw, "---------",
fw, "-----", fw, "---------",
fw, "-----------");
for (i = 0; i < nfreqs; i++)
fprintf(cp_out,
" %-4d %-*s %-*s %-*s %-*s %-*s\n",
i,
fw, pn(freq[i]),
fw, pn(mag[i]),
fw, pn(phase[i]),
fw, pn(nmag[i]),
fw, pn(nphase[i]));
fputs("\n", cp_out);
vec = vec->v_link2;
}
}
free_pnode(first_name);
tfree(freq);
tfree(mag);
tfree(phase);
tfree(nmag);
tfree(nphase);
return;
}
static char *
pn(double num)
{
char buf[BSIZE_SP];
int i = cp_numdgt;
if (i < 1)
i = 6;
if (num < 0.0)
(void) sprintf(buf, "%.*g", i - 1, num);
else
(void) sprintf(buf, "%.*g", i, num);
return (copy(buf));
}
/*
* CKTfour() - perform fourier analysis of an output vector.
* Due to the construction of the program which places all the
* output data in the post-processor, the fourier analysis can not
* be done directly. This function allows the post processor to
* hand back vectors of time and data values to have the fourier analysis
* performed on them.
*
*/
static int
CKTfour(int ndata, int numFreq, double *thd, double *Time, double *Value, double FundFreq, double *Freq, double *Mag, double *Phase, double *nMag, double *nPhase)
/* number of entries in the Time and Value arrays */
/* number of harmonics to calculate */
/* total harmonic distortion (percent) to be returned */
/* times at which the voltage/current values were measured*/
/* voltage or current vector whose transform is desired */
/* the fundamental frequency of the analysis */
/* the frequency value of the various harmonics */
/* the Magnitude of the fourier transform */
/* the Phase of the fourier transform */
/* the normalized magnitude of the transform: nMag(fund)=1*/
/* the normalized phase of the transform: Nphase(fund)=0 */
/* note we can consider these as a set of arrays: The sizes are:
* Time[ndata], Value[ndata]
* Freq[numFreq],Mag[numfreq],Phase[numfreq],nMag[numfreq],nPhase[numfreq]
* The arrays must all be allocated by the caller.
* The Time and Value array must be reasonably distributed over at
* least one full period of the fundamental Frequency for the
* fourier transform to be useful. The function will take the
* last period of the frequency as data for the transform.
*/
{
/* we are assuming that the caller has provided exactly one period
* of the fundamental frequency.
*/
int i;
int j;
double tmp;
/* clear output/computation arrays */
for(i=0;i<numFreq;i++) {
Mag[i]=0;
Phase[i]=0;
}
for(i=0;i<ndata;i++) {
for(j=0;j<numFreq;j++) {
Mag[j] += (Value[i]*sin(j*2.0*M_PI*i/((double) ndata)));
Phase[j] += (Value[i]*cos(j*2.0*M_PI*i/((double) ndata)));
}
}
Mag[0] = Phase[0]/ndata;
Phase[0]=nMag[0]=nPhase[0]=Freq[0]=0;
*thd = 0;
for(i=1;i<numFreq;i++) {
tmp = Mag[i]*2.0 /ndata;
Phase[i] *= 2.0/ndata;
Freq[i] = i * FundFreq;
Mag[i] = sqrt(tmp*tmp+Phase[i]*Phase[i]);
Phase[i] = atan2(Phase[i],tmp)*180.0/M_PI;
nMag[i] = Mag[i]/Mag[1];
nPhase[i] = Phase[i]-Phase[1];
if(i>1) *thd += nMag[i]*nMag[i];
}
*thd = 100*sqrt(*thd);
return(OK);
}