annotate fft/kissfft/tools/kiss_fftr.c @ 40:223f770b5341 kissfft-double tip

Try a double-precision kissfft
author Chris Cannam
date Wed, 07 Sep 2016 10:40:32 +0100
parents 59a1ee198dca
children
rev   line source
Chris@31 1 /*
Chris@31 2 Copyright (c) 2003-2004, Mark Borgerding
Chris@31 3
Chris@31 4 All rights reserved.
Chris@31 5
Chris@31 6 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Chris@31 7
Chris@31 8 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Chris@31 9 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
Chris@31 10 * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission.
Chris@31 11
Chris@31 12 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Chris@31 13 */
Chris@31 14
Chris@31 15 #include "kiss_fftr.h"
Chris@31 16 #include "_kiss_fft_guts.h"
Chris@31 17
Chris@31 18 struct kiss_fftr_state{
Chris@31 19 kiss_fft_cfg substate;
Chris@31 20 kiss_fft_cpx * tmpbuf;
Chris@31 21 kiss_fft_cpx * super_twiddles;
Chris@31 22 #ifdef USE_SIMD
Chris@31 23 void * pad;
Chris@31 24 #endif
Chris@31 25 };
Chris@31 26
Chris@31 27 void kiss_fftr_free(struct kiss_fftr_state *state) {
Chris@31 28 free(state);
Chris@31 29 }
Chris@31 30
Chris@31 31 kiss_fftr_cfg kiss_fftr_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem)
Chris@31 32 {
Chris@31 33 int i;
Chris@31 34 kiss_fftr_cfg st = NULL;
Chris@31 35 size_t subsize, memneeded;
Chris@31 36
Chris@31 37 if (nfft & 1) {
Chris@31 38 fprintf(stderr,"Real FFT optimization must be even.\n");
Chris@31 39 return NULL;
Chris@31 40 }
Chris@31 41 nfft >>= 1;
Chris@31 42
Chris@31 43 kiss_fft_alloc (nfft, inverse_fft, NULL, &subsize);
Chris@31 44 memneeded = sizeof(struct kiss_fftr_state) + subsize + sizeof(kiss_fft_cpx) * ( nfft * 3 / 2);
Chris@31 45
Chris@31 46 if (lenmem == NULL) {
Chris@31 47 st = (kiss_fftr_cfg) KISS_FFT_MALLOC (memneeded);
Chris@31 48 } else {
Chris@31 49 if (*lenmem >= memneeded)
Chris@31 50 st = (kiss_fftr_cfg) mem;
Chris@31 51 *lenmem = memneeded;
Chris@31 52 }
Chris@31 53 if (!st)
Chris@31 54 return NULL;
Chris@31 55
Chris@31 56 st->substate = (kiss_fft_cfg) (st + 1); /*just beyond kiss_fftr_state struct */
Chris@31 57 st->tmpbuf = (kiss_fft_cpx *) (((char *) st->substate) + subsize);
Chris@31 58 st->super_twiddles = st->tmpbuf + nfft;
Chris@31 59 kiss_fft_alloc(nfft, inverse_fft, st->substate, &subsize);
Chris@31 60
Chris@31 61 for (i = 0; i < nfft/2; ++i) {
Chris@31 62 double phase =
Chris@31 63 -3.14159265358979323846264338327 * ((double) (i+1) / nfft + .5);
Chris@31 64 if (inverse_fft)
Chris@31 65 phase *= -1;
Chris@31 66 kf_cexp (st->super_twiddles+i,phase);
Chris@31 67 }
Chris@31 68 return st;
Chris@31 69 }
Chris@31 70
Chris@31 71 void kiss_fftr(kiss_fftr_cfg st,const kiss_fft_scalar *timedata,kiss_fft_cpx *freqdata)
Chris@31 72 {
Chris@31 73 /* input buffer timedata is stored row-wise */
Chris@31 74 int k,ncfft;
Chris@31 75 kiss_fft_cpx fpnk,fpk,f1k,f2k,tw,tdc;
Chris@31 76
Chris@31 77 if ( st->substate->inverse) {
Chris@31 78 fprintf(stderr,"kiss fft usage error: improper alloc\n");
Chris@31 79 exit(1);
Chris@31 80 }
Chris@31 81
Chris@31 82 ncfft = st->substate->nfft;
Chris@31 83
Chris@31 84 /*perform the parallel fft of two real signals packed in real,imag*/
Chris@31 85 kiss_fft( st->substate , (const kiss_fft_cpx*)timedata, st->tmpbuf );
Chris@31 86 /* The real part of the DC element of the frequency spectrum in st->tmpbuf
Chris@31 87 * contains the sum of the even-numbered elements of the input time sequence
Chris@31 88 * The imag part is the sum of the odd-numbered elements
Chris@31 89 *
Chris@31 90 * The sum of tdc.r and tdc.i is the sum of the input time sequence.
Chris@31 91 * yielding DC of input time sequence
Chris@31 92 * The difference of tdc.r - tdc.i is the sum of the input (dot product) [1,-1,1,-1...
Chris@31 93 * yielding Nyquist bin of input time sequence
Chris@31 94 */
Chris@31 95
Chris@31 96 tdc.r = st->tmpbuf[0].r;
Chris@31 97 tdc.i = st->tmpbuf[0].i;
Chris@31 98 C_FIXDIV(tdc,2);
Chris@31 99 CHECK_OVERFLOW_OP(tdc.r ,+, tdc.i);
Chris@31 100 CHECK_OVERFLOW_OP(tdc.r ,-, tdc.i);
Chris@31 101 freqdata[0].r = tdc.r + tdc.i;
Chris@31 102 freqdata[ncfft].r = tdc.r - tdc.i;
Chris@31 103 #ifdef USE_SIMD
Chris@31 104 freqdata[ncfft].i = freqdata[0].i = _mm_set1_ps(0);
Chris@31 105 #else
Chris@31 106 freqdata[ncfft].i = freqdata[0].i = 0;
Chris@31 107 #endif
Chris@31 108
Chris@31 109 for ( k=1;k <= ncfft/2 ; ++k ) {
Chris@31 110 fpk = st->tmpbuf[k];
Chris@31 111 fpnk.r = st->tmpbuf[ncfft-k].r;
Chris@31 112 fpnk.i = - st->tmpbuf[ncfft-k].i;
Chris@31 113 C_FIXDIV(fpk,2);
Chris@31 114 C_FIXDIV(fpnk,2);
Chris@31 115
Chris@31 116 C_ADD( f1k, fpk , fpnk );
Chris@31 117 C_SUB( f2k, fpk , fpnk );
Chris@31 118 C_MUL( tw , f2k , st->super_twiddles[k-1]);
Chris@31 119
Chris@31 120 freqdata[k].r = HALF_OF(f1k.r + tw.r);
Chris@31 121 freqdata[k].i = HALF_OF(f1k.i + tw.i);
Chris@31 122 freqdata[ncfft-k].r = HALF_OF(f1k.r - tw.r);
Chris@31 123 freqdata[ncfft-k].i = HALF_OF(tw.i - f1k.i);
Chris@31 124 }
Chris@31 125 }
Chris@31 126
Chris@31 127 void kiss_fftri(kiss_fftr_cfg st,const kiss_fft_cpx *freqdata,kiss_fft_scalar *timedata)
Chris@31 128 {
Chris@31 129 /* input buffer timedata is stored row-wise */
Chris@31 130 int k, ncfft;
Chris@31 131
Chris@31 132 if (st->substate->inverse == 0) {
Chris@31 133 fprintf (stderr, "kiss fft usage error: improper alloc\n");
Chris@31 134 exit (1);
Chris@31 135 }
Chris@31 136
Chris@31 137 ncfft = st->substate->nfft;
Chris@31 138
Chris@31 139 st->tmpbuf[0].r = freqdata[0].r + freqdata[ncfft].r;
Chris@31 140 st->tmpbuf[0].i = freqdata[0].r - freqdata[ncfft].r;
Chris@31 141 C_FIXDIV(st->tmpbuf[0],2);
Chris@31 142
Chris@31 143 for (k = 1; k <= ncfft / 2; ++k) {
Chris@31 144 kiss_fft_cpx fk, fnkc, fek, fok, tmp;
Chris@31 145 fk = freqdata[k];
Chris@31 146 fnkc.r = freqdata[ncfft - k].r;
Chris@31 147 fnkc.i = -freqdata[ncfft - k].i;
Chris@31 148 C_FIXDIV( fk , 2 );
Chris@31 149 C_FIXDIV( fnkc , 2 );
Chris@31 150
Chris@31 151 C_ADD (fek, fk, fnkc);
Chris@31 152 C_SUB (tmp, fk, fnkc);
Chris@31 153 C_MUL (fok, tmp, st->super_twiddles[k-1]);
Chris@31 154 C_ADD (st->tmpbuf[k], fek, fok);
Chris@31 155 C_SUB (st->tmpbuf[ncfft - k], fek, fok);
Chris@31 156 #ifdef USE_SIMD
Chris@31 157 st->tmpbuf[ncfft - k].i *= _mm_set1_ps(-1.0);
Chris@31 158 #else
Chris@31 159 st->tmpbuf[ncfft - k].i *= -1;
Chris@31 160 #endif
Chris@31 161 }
Chris@31 162 kiss_fft (st->substate, st->tmpbuf, (kiss_fft_cpx *) timedata);
Chris@31 163 }