ngspice/src/frontend/fourier.c

321 lines
10 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/ngspice.h"
#include "ngspice/cpdefs.h"
#include "ngspice/ftedefs.h"
#include "ngspice/dvec.h"
#include "ngspice/fteparse.h"
#include "ngspice/sperror.h"
#include "ngspice/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 char *pnum(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);
2000-04-27 22:03:57 +02:00
#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
2000-04-27 22:03:57 +02:00
*/
int
fourier(wordlist *wl, struct plot *current_plot)
2000-04-27 22:03:57 +02:00
{
struct dvec *time, *vec;
struct pnode *pn, *names;
2000-04-27 22:03:57 +02:00
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;
if (!current_plot)
return 1;
2000-04-27 22:03:57 +02:00
sprintf(xbuf, "%1.1e", 0.0);
shift = (int) strlen(xbuf) - 7;
if (!current_plot || !current_plot->pl_scale) {
2000-04-27 22:03:57 +02:00
fprintf(cp_err, "Error: no vectors loaded.\n");
return 1;
2000-04-27 22:03:57 +02:00
}
2011-04-30 14:29:19 +02:00
if (!cp_getvar("nfreqs", CP_NUM, &nfreqs) || nfreqs < 1)
2000-04-27 22:03:57 +02:00
nfreqs = 10;
2011-04-30 14:29:19 +02:00
if (!cp_getvar("polydegree", CP_NUM, &polydegree) || polydegree < 0)
2000-04-27 22:03:57 +02:00
polydegree = 1;
2011-04-30 14:29:19 +02:00
if (!cp_getvar("fourgridsize", CP_NUM, &fourgridsize) || fourgridsize < 1)
2000-04-27 22:03:57 +02:00
fourgridsize = DEF_FOURGRIDSIZE;
time = current_plot->pl_scale;
2000-04-27 22:03:57 +02:00
if (!isreal(time)) {
fprintf(cp_err, "Error: fourier needs real time scale\n");
return 1;
2000-04-27 22:03:57 +02:00
}
s = wl->wl_word;
if ((ff = ft_numparse(&s, FALSE)) == NULL || (*ff <= 0.0)) {
2000-04-27 22:03:57 +02:00
fprintf(cp_err, "Error: bad fund freq %s\n", wl->wl_word);
return 1;
2000-04-27 22:03:57 +02:00
}
fundfreq = *ff;
freq = TMALLOC(double, nfreqs);
mag = TMALLOC(double, nfreqs);
phase = TMALLOC(double, nfreqs);
nmag = TMALLOC(double, nfreqs);
nphase = TMALLOC(double, nfreqs);
2000-04-27 22:03:57 +02:00
wl = wl->wl_next;
2012-09-30 23:02:25 +02:00
names = ft_getpnames(wl, TRUE);
for (pn = names; pn; pn = pn->pn_next) {
vec = ft_evaluate(pn);
2012-09-30 23:02:25 +02:00
for (; vec; vec = vec->v_link2) {
2000-04-27 22:03:57 +02:00
if (vec->v_length != time->v_length) {
fprintf(cp_err,
"Error: lengths don't match: %d, %d\n",
2000-04-27 22:03:57 +02:00
vec->v_length, time->v_length);
continue;
}
2012-09-30 23:02:25 +02:00
2000-04-27 22:03:57 +02:00
if (!isreal(vec)) {
fprintf(cp_err, "Error: %s isn't real!\n", vec->v_name);
2000-04-27 22:03:57 +02:00
continue;
}
if (polydegree) {
/* Build the grid... */
grid = TMALLOC(double, fourgridsize);
stuff = TMALLOC(double, fourgridsize);
2000-04-27 22:03:57 +02:00
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 1;
2000-04-27 22:03:57 +02:00
} 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;
2000-04-27 22:03:57 +02:00
/* 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");
2012-10-13 11:21:29 +02:00
goto ret_on_err;
2000-04-27 22:03:57 +02:00
}
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);
2000-04-27 22:03:57 +02:00
if (err != OK) {
ft_sperror(err, "fourier");
2012-10-13 11:21:29 +02:00
goto ret_on_err;
2000-04-27 22:03:57 +02:00
}
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);
2000-04-27 22:03:57 +02:00
/* 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",
2000-04-27 22:03:57 +02:00
fw, "Phase", fw, "Norm. Mag",
fw, "Norm. Phase");
fprintf(cp_out, "-------- %-*s %-*s %-*s %-*s %-*s\n",
fw, "---------", fw, "---------",
fw, "-----", fw, "---------",
fw, "-----------");
2012-10-13 11:21:29 +02:00
for (i = 0; i < nfreqs; i++) {
char *pnumfr, *pnumma, *pnumph, *pnumnm, *pnumnp;
pnumfr = pnum(freq[i]);
pnumma = pnum(mag[i]);
pnumph = pnum(phase[i]);
pnumnm = pnum(nmag[i]);
pnumnp = pnum(nphase[i]);
2000-04-27 22:03:57 +02:00
fprintf(cp_out,
" %-4d %-*s %-*s %-*s %-*s %-*s\n",
i,
2012-10-13 11:21:29 +02:00
fw, pnumfr,
fw, pnumma,
fw, pnumph,
fw, pnumnm,
fw, pnumnp);
tfree(pnumfr);
tfree(pnumma);
tfree(pnumph);
tfree(pnumnm);
tfree(pnumnp);
}
2000-04-27 22:03:57 +02:00
fputs("\n", cp_out);
2012-10-13 11:21:29 +02:00
/* generate name for new vector, using vec->name */
/* generate vector of size 3 * nfreqs in current plot */
/* store data in vector freq, mag, phase */
2000-04-27 22:03:57 +02:00
}
}
free_pnode(names);
2000-04-27 22:03:57 +02:00
tfree(freq);
tfree(mag);
tfree(phase);
tfree(nmag);
tfree(nphase);
2012-10-13 11:21:29 +02:00
if (polydegree) {
tfree(grid);
tfree(stuff);
}
return 0;
2012-10-13 11:21:29 +02:00
ret_on_err:
free_pnode(names);
tfree(freq);
tfree(mag);
tfree(phase);
tfree(nmag);
tfree(nphase);
if (polydegree) {
tfree(grid);
tfree(stuff);
}
return 1;
2000-04-27 22:03:57 +02:00
}
void
com_fourier(wordlist *wl)
{
fourier(wl, plot_cur);
}
2000-04-27 22:03:57 +02:00
static char *
pnum(double num)
2000-04-27 22:03:57 +02:00
{
char buf[BSIZE_SP];
int i = cp_numdgt;
if (i < 1)
i = 6;
if (num < 0.0)
sprintf(buf, "%.*g", i - 1, num);
2000-04-27 22:03:57 +02:00
else
sprintf(buf, "%.*g", i, num);
2000-04-27 22:03:57 +02:00
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. */
2000-04-27 22:03:57 +02:00
static int
CKTfour(int ndata, /* number of entries in the Time and
Value arrays */
int numFreq, /* number of harmonics to calculate */
double *thd, /* total harmonic distortion (percent)
to be returned */
double *Time, /* times at which the voltage/current
values were measured*/
double *Value, /* voltage or current vector whose
transform is desired */
double FundFreq, /* the fundamental frequency of the
analysis */
double *Freq, /* the frequency value of the various
harmonics */
double *Mag, /* the Magnitude of the fourier
transform */
double *Phase, /* the Phase of the fourier transform */
double *nMag, /* the normalized magnitude of the
transform: nMag(fund)=1*/
double *nPhase) /* 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]
*
2000-04-27 22:03:57 +02:00
* 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. */
2000-04-27 22:03:57 +02:00
int i;
int j;
double tmp;
2010-11-16 20:11:32 +01:00
2010-11-16 21:38:24 +01:00
NG_IGNORE(Time);
2010-11-16 20:11:32 +01:00
2000-04-27 22:03:57 +02:00
/* clear output/computation arrays */
for (i = 0; i < numFreq; i++) {
Mag[i] = 0;
Phase[i] = 0;
2000-04-27 22:03:57 +02:00
}
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));
2000-04-27 22:03:57 +02:00
}
Mag[0] = Phase[0]/ndata;
Phase[0] = nMag[0] = nPhase[0] = Freq[0] = 0;
2000-04-27 22:03:57 +02:00
*thd = 0;
for (i = 1; i < numFreq; i++) {
tmp = Mag[i] * 2.0 / ndata;
Phase[i] *= 2.0 / ndata;
2000-04-27 22:03:57 +02:00
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];
2000-04-27 22:03:57 +02:00
}
*thd = 100*sqrt(*thd);
return (OK);
2000-04-27 22:03:57 +02:00
}