LCOV - code coverage report
Current view: top level - media/kiss_fft - kiss_fftr.c (source / functions) Hit Total Coverage
Test: output.info Lines: 0 68 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 3 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             : Copyright (c) 2003-2004, Mark Borgerding
       3             : 
       4             : All rights reserved.
       5             : 
       6             : Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
       7             : 
       8             :     * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
       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.
      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.
      11             : 
      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.
      13             : */
      14             : 
      15             : #include "kiss_fftr.h"
      16             : #include "_kiss_fft_guts.h"
      17             : 
      18             : struct kiss_fftr_state{
      19             :     kiss_fft_cfg substate;
      20             :     kiss_fft_cpx * tmpbuf;
      21             :     kiss_fft_cpx * super_twiddles;
      22             : #ifdef USE_SIMD
      23             :     void * pad;
      24             : #endif
      25             : };
      26             : 
      27           0 : kiss_fftr_cfg kiss_fftr_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem)
      28             : {
      29             :     int i;
      30           0 :     kiss_fftr_cfg st = NULL;
      31             :     size_t subsize, memneeded;
      32             : 
      33           0 :     if (nfft & 1) {
      34           0 :         fprintf(stderr,"Real FFT optimization must be even.\n");
      35           0 :         return NULL;
      36             :     }
      37           0 :     nfft >>= 1;
      38             : 
      39           0 :     kiss_fft_alloc (nfft, inverse_fft, NULL, &subsize);
      40           0 :     memneeded = sizeof(struct kiss_fftr_state) + subsize + sizeof(kiss_fft_cpx) * ( nfft * 3 / 2);
      41             : 
      42           0 :     if (lenmem == NULL) {
      43           0 :         st = (kiss_fftr_cfg) KISS_FFT_MALLOC (memneeded);
      44             :     } else {
      45           0 :         if (*lenmem >= memneeded)
      46           0 :             st = (kiss_fftr_cfg) mem;
      47           0 :         *lenmem = memneeded;
      48             :     }
      49           0 :     if (!st)
      50           0 :         return NULL;
      51             : 
      52           0 :     st->substate = (kiss_fft_cfg) (st + 1); /*just beyond kiss_fftr_state struct */
      53           0 :     st->tmpbuf = (kiss_fft_cpx *) (((char *) st->substate) + subsize);
      54           0 :     st->super_twiddles = st->tmpbuf + nfft;
      55           0 :     kiss_fft_alloc(nfft, inverse_fft, st->substate, &subsize);
      56             : 
      57           0 :     for (i = 0; i < nfft/2; ++i) {
      58           0 :         double phase =
      59           0 :             -3.14159265358979323846264338327 * ((double) (i+1) / nfft + .5);
      60           0 :         if (inverse_fft)
      61           0 :             phase *= -1;
      62           0 :         kf_cexp (st->super_twiddles+i,phase);
      63             :     }
      64           0 :     return st;
      65             : }
      66             : 
      67           0 : void kiss_fftr(kiss_fftr_cfg st,const kiss_fft_scalar *timedata,kiss_fft_cpx *freqdata)
      68             : {
      69             :     /* input buffer timedata is stored row-wise */
      70             :     int k,ncfft;
      71             :     kiss_fft_cpx fpnk,fpk,f1k,f2k,tw,tdc;
      72             : 
      73           0 :     if ( st->substate->inverse) {
      74           0 :         fprintf(stderr,"kiss fft usage error: improper alloc\n");
      75           0 :         exit(1);
      76             :     }
      77             : 
      78           0 :     ncfft = st->substate->nfft;
      79             : 
      80             :     /*perform the parallel fft of two real signals packed in real,imag*/
      81           0 :     kiss_fft( st->substate , (const kiss_fft_cpx*)timedata, st->tmpbuf );
      82             :     /* The real part of the DC element of the frequency spectrum in st->tmpbuf
      83             :      * contains the sum of the even-numbered elements of the input time sequence
      84             :      * The imag part is the sum of the odd-numbered elements
      85             :      *
      86             :      * The sum of tdc.r and tdc.i is the sum of the input time sequence. 
      87             :      *      yielding DC of input time sequence
      88             :      * The difference of tdc.r - tdc.i is the sum of the input (dot product) [1,-1,1,-1... 
      89             :      *      yielding Nyquist bin of input time sequence
      90             :      */
      91             :  
      92           0 :     tdc.r = st->tmpbuf[0].r;
      93           0 :     tdc.i = st->tmpbuf[0].i;
      94             :     C_FIXDIV(tdc,2);
      95             :     CHECK_OVERFLOW_OP(tdc.r ,+, tdc.i);
      96             :     CHECK_OVERFLOW_OP(tdc.r ,-, tdc.i);
      97           0 :     freqdata[0].r = tdc.r + tdc.i;
      98           0 :     freqdata[ncfft].r = tdc.r - tdc.i;
      99             : #ifdef USE_SIMD    
     100             :     freqdata[ncfft].i = freqdata[0].i = _mm_set1_ps(0);
     101             : #else
     102           0 :     freqdata[ncfft].i = freqdata[0].i = 0;
     103             : #endif
     104             : 
     105           0 :     for ( k=1;k <= ncfft/2 ; ++k ) {
     106           0 :         fpk    = st->tmpbuf[k]; 
     107           0 :         fpnk.r =   st->tmpbuf[ncfft-k].r;
     108           0 :         fpnk.i = - st->tmpbuf[ncfft-k].i;
     109             :         C_FIXDIV(fpk,2);
     110             :         C_FIXDIV(fpnk,2);
     111             : 
     112           0 :         C_ADD( f1k, fpk , fpnk );
     113           0 :         C_SUB( f2k, fpk , fpnk );
     114           0 :         C_MUL( tw , f2k , st->super_twiddles[k-1]);
     115             : 
     116           0 :         freqdata[k].r = HALF_OF(f1k.r + tw.r);
     117           0 :         freqdata[k].i = HALF_OF(f1k.i + tw.i);
     118           0 :         freqdata[ncfft-k].r = HALF_OF(f1k.r - tw.r);
     119           0 :         freqdata[ncfft-k].i = HALF_OF(tw.i - f1k.i);
     120             :     }
     121           0 : }
     122             : 
     123           0 : void kiss_fftri(kiss_fftr_cfg st,const kiss_fft_cpx *freqdata,kiss_fft_scalar *timedata)
     124             : {
     125             :     /* input buffer timedata is stored row-wise */
     126             :     int k, ncfft;
     127             : 
     128           0 :     if (st->substate->inverse == 0) {
     129           0 :         fprintf (stderr, "kiss fft usage error: improper alloc\n");
     130           0 :         exit (1);
     131             :     }
     132             : 
     133           0 :     ncfft = st->substate->nfft;
     134             : 
     135           0 :     st->tmpbuf[0].r = freqdata[0].r + freqdata[ncfft].r;
     136           0 :     st->tmpbuf[0].i = freqdata[0].r - freqdata[ncfft].r;
     137             :     C_FIXDIV(st->tmpbuf[0],2);
     138             : 
     139           0 :     for (k = 1; k <= ncfft / 2; ++k) {
     140             :         kiss_fft_cpx fk, fnkc, fek, fok, tmp;
     141           0 :         fk = freqdata[k];
     142           0 :         fnkc.r = freqdata[ncfft - k].r;
     143           0 :         fnkc.i = -freqdata[ncfft - k].i;
     144             :         C_FIXDIV( fk , 2 );
     145             :         C_FIXDIV( fnkc , 2 );
     146             : 
     147           0 :         C_ADD (fek, fk, fnkc);
     148           0 :         C_SUB (tmp, fk, fnkc);
     149           0 :         C_MUL (fok, tmp, st->super_twiddles[k-1]);
     150           0 :         C_ADD (st->tmpbuf[k],     fek, fok);
     151           0 :         C_SUB (st->tmpbuf[ncfft - k], fek, fok);
     152             : #ifdef USE_SIMD        
     153             :         st->tmpbuf[ncfft - k].i *= _mm_set1_ps(-1.0);
     154             : #else
     155           0 :         st->tmpbuf[ncfft - k].i *= -1;
     156             : #endif
     157             :     }
     158           0 :     kiss_fft (st->substate, st->tmpbuf, (kiss_fft_cpx *) timedata);
     159           0 : }

Generated by: LCOV version 1.13