📄 fft.c.sh
字号:
count=$1case $count in996) :;;*) echo 'Bad character count in ''fft/complex/w.c' >&2 echo 'Count should be 996' >&2esacecho Extracting 'fft/complex/w.h'sed 's/^X//' > 'fft/complex/w.h' << '+ END-OF-FILE ''fft/complex/w.h'X/*X * "w.h", Pjotr '87.X */XXextern COMPLEX *W_factors;Xextern unsigned Nfactors;XX/*X * W gives the (already computed) Wn ^ k (= e ^ (2pi * i * k / n)).X * Notice that the powerseries of Wn has period Nfactors.X */X#define W(n, k) (W_factors [((k) * (Nfactors / (n))) % Nfactors])+ END-OF-FILE fft/complex/w.hchmod 'u=rw,g=r,o=r' 'fft/complex/w.h'set `wc -c 'fft/complex/w.h'`count=$1case $count in283) :;;*) echo 'Bad character count in ''fft/complex/w.h' >&2 echo 'Count should be 283' >&2esacecho Extracting 'fft/complex.h'sed 's/^X//' > 'fft/complex.h' << '+ END-OF-FILE ''fft/complex.h'X/*X * "complex.h", Pjotr '87.X */XXtypedef struct {X double re, im;X } COMPLEX;X#define c_re(c) ((c).re)X#define c_im(c) ((c).im)XX/*X * C_add_mul adds product of c1 and c2 to c.X */X#define c_add_mul(c, c1, c2) { COMPLEX C1, C2; C1 = (c1); C2 = (c2); \X c_re (c) += C1.re * C2.re - C1.im * C2.im; \X c_im (c) += C1.re * C2.im + C1.im * C2.re; }XX/*X * C_conj substitutes c by its complex conjugate.X */X#define c_conj(c) { c_im (c) = -c_im (c); }XX/*X * C_realdiv divides complex c by real.X */X#define c_realdiv(c, real) { c_re (c) /= (real); c_im (c) /= (real); }+ END-OF-FILE fft/complex.hchmod 'u=rw,g=r,o=r' 'fft/complex.h'set `wc -c 'fft/complex.h'`count=$1case $count in583) :;;*) echo 'Bad character count in ''fft/complex.h' >&2 echo 'Count should be 583' >&2esacif test -f 'fft/example'then echo Removing 'fft/example' rm 'fft/example'fiif test -d 'fft/example'then :else echo Creating 'fft/example' mkdir 'fft/example'fichmod 'u=rwx,g=rx,o=rx' 'fft/example'echo Extracting 'fft/example/README'sed 's/^X//' > 'fft/example/README' << '+ END-OF-FILE ''fft/example/README'XAn example of realfft(3) usage is in example.c. It contains two fourierXtransforms on real data.XXCompile with:X cc example.c ../real/realfft.a ../complex/fft.a -lm+ END-OF-FILE fft/example/READMEchmod 'u=rw,g=r,o=r' 'fft/example/README'set `wc -c 'fft/example/README'`count=$1case $count in166) :;;*) echo 'Bad character count in ''fft/example/README' >&2 echo 'Count should be 166' >&2esacecho Extracting 'fft/example/example.c'sed 's/^X//' > 'fft/example/example.c' << '+ END-OF-FILE ''fft/example/example.c'X/*X * Test for realfft(3).X */XX#define N 8X#define pi 3.1415926535897932384626434XXdouble sin (), cos ();Xchar *malloc ();XXmain ()X{X unsigned i, j;XX double in [N], out [N];XX printf ("Example #1:\n");X for (i = 0; i < N; i++)X in [i] = i;X printsamp (in, N);XX realfft (in, N, out);X printf ("After a fast fft\n");X printampl (out, N);XX /* check */X printf ("A reverse slow ft gives:\n");X srft (out, N, in);X printsamp (in, N);XX printf ("And the reverse fast ft yields:\n");X realrft (out, N, in);X printsamp (in, N);XX printf ("\n\nExample #2\n");X for (i = 0; i < N; i++) {X in [i] = 0;X for (j = 0; j <= N / 2; j++)X in [i] += cos (2 * pi * i * j / N) +X sin (2 * pi * i * j / N);X }X printsamp (in, N);XX realfft (in, N, out);X printf ("After a forward fast ft:\n");X printampl (out, N);XX /* check */X printf ("A reverse slow ft yields:\n");X srft (out, N, in);X printsamp (in, N);XX printf ("And a reverse fast ft gives:\n");X realrft (out, N, in);X printsamp (in, N);X}XXprintampl (ampl, n)Xdouble *ampl;Xunsigned n;X{X unsigned i;XX printf ("Amplitudes\n");XX if (n == 0)X return;XX printf ("%f (dc)\n", ampl [0]);X for (i = 1; i < (n + 1) / 2; i++)X printf ("%f, %f (%u-th harmonic)\n", ampl [2 * i - 1],X ampl [2 * i], i);X if (n % 2 == 0)X printf ("%f (Nyquist)\n", ampl [n - 1]);XX printf ("\n");X}XXprintsamp (samp, n)Xdouble *samp;Xunsigned n;X{X unsigned i;X printf ("Samples\n");XX for (i = 0; i < n; i++)X printf ("%f\n", samp [i]);X X printf ("\n");X}XX/*X * Slow reverse fourier transform. In [0] contains dc, in [n - 1] Nyquist.X * This is just a gimmick to compare with realfft(3).X */Xsrft (in, n, out)Xdouble *in;Xunsigned n;Xdouble *out;X{X unsigned i, j;XX for (i = 0; i < n; i++) {X out [i] = in [0]; /* dc */X for (j = 1; j < (n + 1) / 2; j++) /* j-th harmonic */X out [i] += in [2 * j - 1] * cos (2 * pi * i * j / n) +X in [2 * j] * sin (2 * pi * i * j / n);X if (n % 2 == 0) /* Nyquist */X out [i] += in [n - 1] * cos (2 * pi * i * j / n);X }X}+ END-OF-FILE fft/example/example.cchmod 'u=rw,g=r,o=r' 'fft/example/example.c'set `wc -c 'fft/example/example.c'`count=$1case $count in2026) :;;*) echo 'Bad character count in ''fft/example/example.c' >&2 echo 'Count should be 2026' >&2esacif test -f 'fft/real'then echo Removing 'fft/real' rm 'fft/real'fiif test -d 'fft/real'then :else echo Creating 'fft/real' mkdir 'fft/real'fichmod 'u=rwx,g=rx,o=rx' 'fft/real'echo Extracting 'fft/real/Makefile'sed 's/^X//' > 'fft/real/Makefile' << '+ END-OF-FILE ''fft/real/Makefile'XLTYPE=A18XTARGET=realfft.aXCFLAGS=-OXPREFLAGS=XIDIRS=-I../XLLIBS=XXLINT=lint -uhbaxcXCTAGS=ctagsXPRINT=@pr -tXXCFILES=\X realfft.cXHFILES=\X ../complex.hXOBJECTS=\X realfft.oXX.SUFFIXES: .iXX$(TARGET): $(OBJECTS)X ar rv $@ $?X ranlib $@XXlint:X $(LINT) $(PREFLAGS) $(IDIRS) $(CFILES) $(LLIBS) -lcXXtags: $(HFILES) $(CFILES)X $(CTAGS) $(HFILES) $(CFILES)XXprint: realfft.manX $(PRINT) realfft.man $(CFILES)XXrealfft.man: realfft.3X @nroff -man realfft.3 > realfft.manXX.c.o:X $(CC) $(CFLAGS) -c $(IDIRS) $<XX.c.i:X $(CC) $(CFLAGS) -P $(IDIRS) $<XX.c.s:X $(CC) $(CFLAGS) -S $(IDIRS) $<XXrealfft.o:\X ../complex.h+ END-OF-FILE fft/real/Makefilechmod 'u=rw,g=r,o=r' 'fft/real/Makefile'set `wc -c 'fft/real/Makefile'`count=$1case $count in611) :;;*) echo 'Bad character count in ''fft/real/Makefile' >&2 echo 'Count should be 611' >&2esacecho Extracting 'fft/real/README'sed 's/^X//' > 'fft/real/README' << '+ END-OF-FILE ''fft/real/README'XThis realfft(3) package does Cooley-Tukey fast Fourier transform on an arbitraryXnumber of real samples. It uses fft(3) for the actual complex transform.XXHow to make the stuff:X make - create library "realfft.a"X make fft.man - nroff manual page "realfft.3" into "realfft.man"X make print - print source and "realfft.man"XXPrograms using realfft(3), should be loaded (ld(1)) with libraries "realfft.a",X"../complex/fft.a" and "/usr/lib/libm.a". The package also uses malloc(3) ofXthe standard C-library.+ END-OF-FILE fft/real/READMEchmod 'u=rw,g=r,o=r' 'fft/real/README'set `wc -c 'fft/real/README'`count=$1case $count in508) :;;*) echo 'Bad character count in ''fft/real/README' >&2 echo 'Count should be 508' >&2esacecho Extracting 'fft/real/realfft.3'sed 's/^X//' > 'fft/real/realfft.3' << '+ END-OF-FILE ''fft/real/realfft.3'X.TH REALFFT 3X.SH NAMEXrealfft, realrft \- forward and reverse real fourier transformX.SH SYNOPSISX.nfXrealfft (in, n, out)Xdouble *in;Xunsigned n;Xdouble *out;XXrealrft (in, n, out)Xdouble *in;Xunsigned n;Xdouble *out;X.fiX.SH DESCRIPTIONX.I RealfftXandX.I realrftXperform, respectively, forward and reverse discreteXfast fourier transform on theX.I nX(an arbitrary number) realsXof arrayX.IR in .XThe result (alsoX.I nXreals) is placed in arrayX.IR out .XThe original contents ofX.I inXare not disturbed.XXThe format of theX.I outXarray ofX.I realfftXand theX.I inXarray ofX.I realrftXis as follows:XThe cosine component of the dc frequency is under index 0Xand theX.IR i -thXharmonic's cosine and sine are under, respectively, indexX2 *X.I iX- 1 and 2 *X.IR i .XIfX.I nXis even then the cosine component of theXNyquist frequency is under indexX.I nX- 1.XNote that the dc and Nyquist sine components need not be passed, since theyXare always 0.XXThe actual transform is done byX.IR fft (3)Xin complex space.X.SH "SEE ALSO"Xfft(3)X.SH FILESXrealfft.a \- contains realfft and realrft.X.brXfft.a \- library used by realfft.a.X.brX/usr/lib/libm.a \- library used by fft.a.X.SH DIAGNOSTICSX.I RealfftXandX.I realrftXreturn -1 if routines inX.IR fft (3)Xfail, else 0.X.SH BUGSXThe error for a forward-reverse sequence is about 1e-13 forX.I nX= 1024.X.SH AUTHORXPeter Valkenburg (valke@cs.vu.nl)+ END-OF-FILE fft/real/realfft.3chmod 'u=rw,g=r,o=r' 'fft/real/realfft.3'set `wc -c 'fft/real/realfft.3'`count=$1case $count in1391) :;;*) echo 'Bad character count in ''fft/real/realfft.3' >&2 echo 'Count should be 1391' >&2esacecho Extracting 'fft/real/realfft.c'sed 's/^X//' > 'fft/real/realfft.c' << '+ END-OF-FILE ''fft/real/realfft.c'X/*X * "realfft.c", Pjotr '87X */XX/*X * Bevat funkties realfft en realrft die resp. forward en reverse fast fourierX * transform op reele samples doen. Gebruikt pakket fft(3).X */XX#include <complex.h>XXchar *malloc ();XX/*X * Reele forward fast fourier transform van n samples van in naarX * amplitudes van out.X * De cosinus komponent van de dc komt in out [0], dan volgen inX * out [2 * i - 1] en out [2 * i] steeds resp. de cosinus en sinusX * komponenten van de i-de harmonische. Bij een even aantal samplesX * bevat out [n - 1] de cosinus komponent van de Nyquist frequentie. X * Extraatje: Na afloop is in onaangetast.X */Xrealfft (in, n, out)Xdouble *in;Xunsigned n;Xdouble *out;X{X COMPLEX *c_in, *c_out;X unsigned i;XX if (n == 0 ||X (c_in = (COMPLEX *) malloc (n * sizeof (COMPLEX))) == 0 ||X (c_out = (COMPLEX *) malloc (n * sizeof (COMPLEX))) == 0)X return;X X for (i = 0; i < n; i++) {X c_re (c_in [i]) = in [i];X c_im (c_in [i]) = 0;X }XX fft (c_in, n, c_out);XX out [0] = c_re (c_out [0]); /* cos van dc */X for (i = 1; i < (n + 1) / 2; i++) { /* cos/sin i-de harmonische */X out [2 * i - 1] = c_re (c_out [i]) * 2;X out [2 * i] = c_im (c_out [i]) * -2;X }X if (n % 2 == 0) /* cos van Nyquist */X out [n - 1] = c_re (c_out [n / 2]);XX free ((char *) c_in);X free ((char *) c_out);X}XX/*X * Reele reverse fast fourier transform van amplitudes van in naarX * n samples van out.X * De cosinus komponent van de dc staat in in [0], dan volgen inX * in [2 * i - 1] en in [2 * i] steeds resp. de cosinus en sinusX * komponenten van de i-de harmonische. Bij een even aantal samplesX * bevat in [n - 1] de cosinus komponent van de Nyquist frequentie. X * Extraatje: Na afloop is in onaangetast.X */Xrealrft (in, n, out)Xdouble *in;Xunsigned n;Xdouble *out;X{X COMPLEX *c_in, *c_out;X unsigned i;XX if (n == 0 ||X (c_in = (COMPLEX *) malloc (n * sizeof (COMPLEX))) == 0 ||X (c_out = (COMPLEX *) malloc (n * sizeof (COMPLEX))) == 0)X return;X X c_re (c_in [0]) = in [0]; /* dc */X c_im (c_in [0]) = 0;X for (i = 1; i < (n + 1) / 2; i++) { /* geconj. symm. harmonischen */X c_re (c_in [i]) = in [2 * i - 1] / 2;X c_im (c_in [i]) = in [2 * i] / -2;X c_re (c_in [n - i]) = in [2 * i - 1] / 2;X c_im (c_in [n - i]) = in [2 * i] / 2;X }X if (n % 2 == 0) { /* Nyquist */X c_re (c_in [n / 2]) = in [n - 1];X c_im (c_in [n / 2]) = 0;X }XX rft (c_in, n, c_out);XX for (i = 0; i < n; i++)X out [i] = c_re (c_out [i]);XX free ((char *) c_in);X free ((char *) c_out);X}+ END-OF-FILE fft/real/realfft.cchmod 'u=rw,g=r,o=r' 'fft/real/realfft.c'set `wc -c 'fft/real/realfft.c'`count=$1case $count in2503) :;;*) echo 'Bad character count in ''fft/real/realfft.c' >&2 echo 'Count should be 2503' >&2esacexit 0
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -