bunch of minor code cleanup

This commit is contained in:
Mark Borgerding
2004-02-27 03:45:12 +00:00
parent 0fd8da731a
commit e3fe1598f0
17 changed files with 161 additions and 130 deletions

View File

@ -1,3 +1,7 @@
WARNINGS=-W -Wall -Wstrict-prototypes -Wmissing-prototypes -Waggregate-return \
-Wcast-align -Wcast-qual -Wnested-externs -Wshadow -Wbad-function-cast \
-Wwrite-strings
ifeq "$(DATATYPE)" ""
DATATYPE=float
endif
@ -23,7 +27,7 @@ endif
all: $(FFTUTIL) $(FASTFILT) $(FASTFILTREAL) $(PSDPNG)
CFLAGS=-Wall -O3 -pedantic -march=pentiumpro -ffast-math -fomit-frame-pointer
CFLAGS=-Wall -O3 -pedantic -march=pentiumpro -ffast-math -fomit-frame-pointer $(WARNINGS)
# If the above flags do not work, try the following
#CFLAGS=-Wall -O3

View File

@ -22,6 +22,7 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
#include "kiss_fftnd.h"
#include "kiss_fftr.h"
static
void fft_file(FILE * fin,FILE * fout,int nfft,int isinverse)
{
kiss_fft_cfg st;
@ -41,9 +42,10 @@ void fft_file(FILE * fin,FILE * fout,int nfft,int isinverse)
free(bufout);
}
static
void fft_filend(FILE * fin,FILE * fout,int *dims,int ndims,int isinverse)
{
void *st;
kiss_fftnd_cfg st;
kiss_fft_cpx *buf;
int dimprod=1,i;
for (i=0;i<ndims;++i)
@ -60,6 +62,7 @@ void fft_filend(FILE * fin,FILE * fout,int *dims,int ndims,int isinverse)
free (buf);
}
static
void fft_file_real(FILE * fin,FILE * fout,int nfft,int isinverse)
{
kiss_fftr_cfg st;
@ -86,6 +89,7 @@ void fft_file_real(FILE * fin,FILE * fout,int nfft,int isinverse)
free(cbuf);
}
static
int get_dims(char * arg,int * dims)
{
char *p0;

View File

@ -14,32 +14,35 @@ Redistribution and use in source and binary forms, with or without modification,
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.
*/
typedef struct
typedef struct cached_fft *kfc_cfg;
struct cached_fft
{
int nfft;
int inverse;
kiss_fft_cfg cfg;
void * next;
} cached_fft;
kfc_cfg next;
};
static cached_fft *cache_root=NULL;
static kfc_cfg cache_root=NULL;
static int ncached=0;
static kiss_fft_cfg find_cached_fft(int nfft,int inverse)
{
size_t len;
cached_fft * cur=cache_root;
cached_fft * prev=NULL;
kfc_cfg cur=cache_root;
kfc_cfg prev=NULL;
while ( cur ) {
if ( cur->nfft == nfft && inverse == cur->inverse )
break;/*found the right node*/
prev = cur;
cur = (cached_fft*)prev->next;
cur = prev->next;
}
if (cur== NULL) {
/* no cached node found, need to create a new one*/
kiss_fft_alloc(nfft,inverse,0,&len);
cur = (cached_fft*)malloc(sizeof(cached_fft) + len );
cur = (kfc_cfg)malloc(sizeof(struct cached_fft) + len );
if (cur == NULL)
return NULL;
cur->cfg = (kiss_fft_cfg)(cur+1);
@ -56,14 +59,14 @@ static kiss_fft_cfg find_cached_fft(int nfft,int inverse)
return cur->cfg;
}
void kfc_cleanup()
void kfc_cleanup(void)
{
cached_fft * cur=cache_root;
cached_fft * next=NULL;
kfc_cfg cur=cache_root;
kfc_cfg next=NULL;
while (cur){
next = (cached_fft*)cur->next;
next = cur->next;
free(cur);
cur=(cached_fft*)next;
cur=next;
}
ncached=0;
cache_root = NULL;
@ -87,7 +90,7 @@ static void check(int nc)
}
}
int main(int argc,char ** argv)
int main(void)
{
kiss_fft_cpx buf1[1024],buf2[1024];
memset(buf1,0,sizeof(buf1));

View File

@ -37,7 +37,7 @@ void kfc_fft(int nfft, const kiss_fft_cpx * fin,kiss_fft_cpx * fout);
void kfc_ifft(int nfft, const kiss_fft_cpx * fin,kiss_fft_cpx * fout);
/*free all cached objects*/
void kfc_cleanup();
void kfc_cleanup(void);
#ifdef __cplusplus
}

View File

@ -22,50 +22,57 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
#define MIN_FFT_LEN 2048
#include "kiss_fftr.h"
typedef kiss_fft_scalar kffsamp_t;
typedef kiss_fftr_cfg kfcfg_t;
#define FFT_ALLOC kiss_fftr_alloc
#define FFTFWD kiss_fftr
#define FFTINV kiss_fftri
#else
#define MIN_FFT_LEN 1024
typedef kiss_fft_cpx kffsamp_t;
typedef kiss_fft_cfg kfcfg_t;
#define FFT_ALLOC kiss_fft_alloc
#define FFTFWD kiss_fft
#define FFTINV kiss_fft
#endif
typedef struct kiss_fastfir_state *kiss_fastfir_cfg;
void * kiss_fastfir_alloc(const kffsamp_t * imp_resp,size_t n_imp_resp,
kiss_fastfir_cfg kiss_fastfir_alloc(const kffsamp_t * imp_resp,size_t n_imp_resp,
size_t * nfft,void * mem,size_t*lenmem);
/* see do_file_filter for usage */
size_t kiss_fastfir( void * cfg, kffsamp_t * inbuf, kffsamp_t * outbuf, size_t n, size_t *offset);
size_t kiss_fastfir( kiss_fastfir_cfg cfg, kffsamp_t * inbuf, kffsamp_t * outbuf, size_t n, size_t *offset);
static int verbose=0;
typedef struct {
int nfft;
struct kiss_fastfir_state{
size_t nfft;
size_t ngood;
void * fftcfg;
void * ifftcfg;
kfcfg_t fftcfg;
kfcfg_t ifftcfg;
kiss_fft_cpx * fir_freq_resp;
kiss_fft_cpx * freqbuf;
size_t n_freq_bins;
kffsamp_t * tmpbuf;
}kiss_fastfir_state;
};
void * kiss_fastfir_alloc(
kiss_fastfir_cfg kiss_fastfir_alloc(
const kffsamp_t * imp_resp,size_t n_imp_resp,
size_t *pnfft, /* if <= 0, an appropriate size will be chosen */
void * mem,size_t*lenmem)
{
kiss_fastfir_state *st = NULL;
kiss_fastfir_cfg st = NULL;
size_t len_fftcfg,len_ifftcfg;
size_t memneeded = sizeof(kiss_fastfir_state);
size_t memneeded = sizeof(struct kiss_fastfir_state);
char * ptr;
size_t i;
int nfft=0;
size_t nfft=0;
float scale;
int n_freq_bins;
if (pnfft)
@ -106,10 +113,10 @@ void * kiss_fastfir_alloc(
memneeded += sizeof(kiss_fft_cpx) * n_freq_bins;
if (lenmem == NULL) {
st = (kiss_fastfir_state *) malloc (memneeded);
st = (kiss_fastfir_cfg) malloc (memneeded);
} else {
if (*lenmem >= memneeded)
st = (kiss_fastfir_state *) mem;
st = (kiss_fastfir_cfg) mem;
*lenmem = memneeded;
}
if (!st)
@ -120,10 +127,10 @@ void * kiss_fastfir_alloc(
st->n_freq_bins = n_freq_bins;
ptr=(char*)(st+1);
st->fftcfg = (void*)ptr;
st->fftcfg = (kfcfg_t)ptr;
ptr += len_fftcfg;
st->ifftcfg = (void*)ptr;
st->ifftcfg = (kfcfg_t)ptr;
ptr += len_ifftcfg;
st->tmpbuf = (kffsamp_t*)ptr;
@ -158,9 +165,9 @@ void * kiss_fastfir_alloc(
return st;
}
static void fastconv1buf(const kiss_fastfir_state *st,const kffsamp_t * in,kffsamp_t * out)
static void fastconv1buf(const kiss_fastfir_cfg st,const kffsamp_t * in,kffsamp_t * out)
{
int i;
size_t i;
/* multiply the frequency response of the input signal by
that of the fir filter*/
FFTFWD( st->fftcfg, in , st->freqbuf );
@ -178,12 +185,11 @@ static void fastconv1buf(const kiss_fastfir_state *st,const kffsamp_t * in,kffsa
return value: the number of samples completely processed
n-retval samples should be copied to the front of the next input buffer */
static size_t kff_nocopy(
void *vst,
kiss_fastfir_cfg st,
const kffsamp_t * inbuf,
kffsamp_t * outbuf,
size_t n)
{
kiss_fastfir_state *st=(kiss_fastfir_state *)vst;
size_t norig=n;
while (n >= st->nfft ) {
fastconv1buf(st,inbuf,outbuf);
@ -195,12 +201,11 @@ static size_t kff_nocopy(
}
static
size_t kff_flush(void *vst,const kffsamp_t * inbuf,kffsamp_t * outbuf,size_t n)
size_t kff_flush(kiss_fastfir_cfg st,const kffsamp_t * inbuf,kffsamp_t * outbuf,size_t n)
{
size_t zpad=0,ntmp;
kiss_fastfir_state *st=(kiss_fastfir_state *)vst;
ntmp = kff_nocopy(vst,inbuf,outbuf,n);
ntmp = kff_nocopy(st,inbuf,outbuf,n);
n -= ntmp;
inbuf += ntmp;
outbuf += ntmp;
@ -216,7 +221,7 @@ size_t kff_flush(void *vst,const kffsamp_t * inbuf,kffsamp_t * outbuf,size_t n)
}
size_t kiss_fastfir(
void * vst,
kiss_fastfir_cfg vst,
kffsamp_t * inbuf,
kffsamp_t * outbuf,
size_t n_new,
@ -240,12 +245,12 @@ size_t kiss_fastfir(
#include <sys/mman.h>
#include <assert.h>
static
void direct_file_filter(
FILE * fin,
FILE * fout,
const kffsamp_t * imp_resp,
size_t n_imp_resp,
size_t nfft )
size_t n_imp_resp)
{
size_t nlag = n_imp_resp - 1;
@ -320,7 +325,7 @@ void direct_file_filter(
free (circbuf);
}
static
void do_file_filter(
FILE * fin,
FILE * fout,
@ -328,12 +333,12 @@ void do_file_filter(
size_t n_imp_resp,
size_t nfft )
{
int fdin,fdout;
int fdout;
size_t n_samps_buf;
void * cfg;
kiss_fastfir_cfg cfg;
kffsamp_t *inbuf,*outbuf;
size_t nread,nwrite;
int nread,nwrite;
size_t idx_inbuf;
fdout = fileno(fout);
@ -437,7 +442,7 @@ int main(int argc,char**argv)
fclose(filtfile);
if (use_direct)
direct_file_filter( fin, fout, h,nh,nfft);
direct_file_filter( fin, fout, h,nh);
else
do_file_filter( fin, fout, h,nh,nfft);

View File

@ -56,7 +56,7 @@ kiss_fftr_cfg kiss_fftr_alloc(int nfft,int inverse_fft,void * mem,size_t * lenme
-3.14159265358979323846264338327 * ((double) i / nfft + .5);
if (inverse_fft)
phase *= -1;
st->super_twiddles[i] = kf_cexp (phase);
kf_cexp (st->super_twiddles+i,phase);
}
return st;
}

View File

@ -22,8 +22,8 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
#include "kiss_fft.h"
#include "kiss_fftr.h"
int nfft=1024;
int nfreqs=0;
size_t nfft=1024;
size_t nfreqs=0;
int repeat=0;
int window=0;
int colors=256;
@ -37,6 +37,7 @@ int remove_dc=1;
size_t nrows=0;
float * vals=NULL;
static
void config(int argc,char** argv)
{
while (1) {
@ -45,7 +46,7 @@ void config(int argc,char** argv)
break;
switch (c) {
case 'n':
nfft=atoi(optarg);
nfft=(size_t)atoi(optarg);
case '?':
fprintf (stderr, "usage options:\n"
"\t-n d: fft dimension(s) default = 1024\n"
@ -83,45 +84,43 @@ typedef struct
png_byte b;
} rgb_t;
rgb_t val2rgb(float x)
static
void val2rgb(float x,rgb_t *p)
{
const double pi = 3.14159265358979;
rgb_t p;
p.g = (int)(255*sin(x*pi));
p.r = (int)(255*abs(sin(x*pi*3/2)));
p.b = (int)(255*abs(sin(x*pi*5/2)));
return p;
p->g = (int)(255*sin(x*pi));
p->r = (int)(255*abs(sin(x*pi*3/2)));
p->b = (int)(255*abs(sin(x*pi*5/2)));
}
void cpx2pixels(rgb_t * res,const float * fbuf,size_t nfreqs)
static
void cpx2pixels(rgb_t * res,const float * fbuf,size_t n)
{
int i;
size_t i;
float minval,maxval,valrange;
minval=maxval=fbuf[0];
for (i = 0; i < nfreqs; ++i) {
for (i = 0; i < n; ++i) {
if (fbuf[i] > maxval) maxval = fbuf[i];
if (fbuf[i] < minval) minval = fbuf[i];
}
valrange = maxval-minval;
for (i = 0; i < nfreqs; ++i)
res[i] = val2rgb( (fbuf[i] - minval)/valrange );
for (i = 0; i < n; ++i)
val2rgb( (fbuf[i] - minval)/valrange , res+i );
}
void transform_signal()
static
void transform_signal(void)
{
short *inbuf;
void * cfg=NULL;
kiss_fftr_cfg cfg=NULL;
kiss_fft_scalar *tbuf;
kiss_fft_cpx *fbuf;
kiss_fft_cpx *avgbuf;
size_t i;
int n;
size_t n;
int avgctr=0;
nfreqs=nfft/2+1;
@ -132,7 +131,11 @@ void transform_signal()
CHECKNULL( fbuf=(kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx)*nfreqs ) );
CHECKNULL( avgbuf=(kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx)*nfreqs ) );
while ( ( n = fread(inbuf,sizeof(short)*2,nfft,fin) ) == nfft ) {
while (1){
n = fread(inbuf,sizeof(short)*2,nfft,fin);
if (n != nfft )
break;
/* pack the shorts */
for (i=0;i<nfft;++i){
tbuf[i] = inbuf[2*i] + inbuf[2*i+1];
@ -172,7 +175,8 @@ void transform_signal()
free(avgbuf);
}
void make_png()
static
void make_png(void)
{
png_bytepp row_pointers=NULL;
rgb_t * row_data=NULL;