File indexing completed on 2024-06-09 04:49:45

0001 /*
0002 SPDX-FileCopyrightText: 2003-2004 Mark Borgerding <Mark@Borgerding.net>
0003 SPDX-License-Identifier: BSD-3-Clause
0004 
0005 All rights reserved.
0006 
0007 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
0008 
0009     * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
0010     * 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.
0011     * 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.
0012 
0013 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.
0014 */
0015 
0016 #include "kfc.h"
0017 
0018 
0019 typedef struct cached_fft *kfc_cfg;
0020 
0021 struct cached_fft
0022 {
0023     int nfft;
0024     int inverse;
0025     kiss_fft_cfg cfg;
0026     kfc_cfg next;
0027 };
0028 
0029 static kfc_cfg cache_root=NULL;
0030 static int ncached=0;
0031 
0032 static kiss_fft_cfg find_cached_fft(int nfft,int inverse)
0033 {
0034     size_t len;
0035     kfc_cfg  cur=cache_root;
0036     kfc_cfg  prev=NULL;
0037     while ( cur ) {
0038         if ( cur->nfft == nfft && inverse == cur->inverse )
0039             break;/*found the right node*/
0040         prev = cur;
0041         cur = prev->next;
0042     }
0043     if (cur== NULL) {
0044         /* no cached node found, need to create a new one*/
0045         kiss_fft_alloc(nfft,inverse,0,&len);
0046         cur = (kfc_cfg)KISS_FFT_MALLOC((sizeof(struct cached_fft) + len ));
0047         if (cur == NULL)
0048             return NULL;
0049         cur->cfg = (kiss_fft_cfg)(cur+1);
0050         kiss_fft_alloc(nfft,inverse,cur->cfg,&len);
0051         cur->nfft=nfft;
0052         cur->inverse=inverse;
0053         cur->next = NULL;
0054         if ( prev )
0055             prev->next = cur;
0056         else
0057             cache_root = cur;
0058         ++ncached;
0059     }
0060     return cur->cfg;
0061 }
0062 
0063 void kfc_cleanup(void)
0064 {
0065     kfc_cfg  cur=cache_root;
0066     kfc_cfg  next=NULL;
0067     while (cur){
0068         next = cur->next;
0069         free(cur);
0070         cur=next;
0071     }
0072     ncached=0;
0073     cache_root = NULL;
0074 }
0075 void kfc_fft(int nfft, const kiss_fft_cpx * fin,kiss_fft_cpx * fout)
0076 {
0077     kiss_fft( find_cached_fft(nfft,0),fin,fout );
0078 }
0079 
0080 void kfc_ifft(int nfft, const kiss_fft_cpx * fin,kiss_fft_cpx * fout)
0081 {
0082     kiss_fft( find_cached_fft(nfft,1),fin,fout );
0083 }
0084 
0085 #ifdef KFC_TEST
0086 static void check(int nc)
0087 {
0088     if (ncached != nc) {
0089         fprintf(stderr,"ncached should be %d,but it is %d\n",nc,ncached);
0090         exit(1);
0091     }
0092 }
0093 
0094 int main(void)
0095 {
0096     kiss_fft_cpx buf1[1024],buf2[1024];
0097     memset(buf1,0,sizeof(buf1));
0098     check(0);
0099     kfc_fft(512,buf1,buf2);
0100     check(1);
0101     kfc_fft(512,buf1,buf2);
0102     check(1);
0103     kfc_ifft(512,buf1,buf2);
0104     check(2);
0105     kfc_cleanup();
0106     check(0);
0107     return 0;
0108 }
0109 #endif