Mail Archives: djgpp/2003/06/17/00:34:25
Ethan Rosenberg<ethros AT earthlink DOT net> wrote [heavily snipped]:
>
#include <stdlib.h> /* for malloc(), free(), exit() */
>
> void main(void)
> {
main() returns int.
> FLOAT data[SAMPLES],sum1,sum2,avg,index,freqavg, fft_out[FFT_LEN/2],temp;
> FLOAT temp2[FFT_LEN],e_low,e_high,e_tot,delta,min,delta_avg,std_dev;
> FLOAT avg_del[30],minplus;
> complx wave[FFT_LEN];
> long pos_byte,pos;
> struct jha_data
> {
> short fhr;
> short toco;
> short emg;
> };
> struct jha_data data_in[SAMPLES];
These arrays take up a lot of stack space. Maybe you should malloc
them instead.
>
> printf("Enter Input Data File [including path]: ");
> gets(string2);
Nobody in their right mind use gets(). I have plenty of paths longer
that 31 characters.
> for (i=0;i<31;i++)
> avg_del[i]=0.0;
You only have 30 elements in avg_del[].
> if( (fptr1 = fopen(string2, "rb")) == NULL)
> {
> printf("\x7");
I dislike noisy programs, but if you must, use "\a" (alert).
There's no telling what "\x7" does on other platforms.
> perror("Error in opening source file\n");
> fclose(fptr1);
You cannot close what has not been opened.
> while(!feof(fptr1))
> {
This is usually a mistake because feof() only detects end-of-file
/after/ an input operation has failed. I don't think it does
anything useful in this case, though. You could just as well
replace it with while(1).
> fseek(fptr1,-(SAMPLES-STEP)*sizeof(FLOAT),SEEK_CUR);
-(SAMPLES-STEP) will (most likely) be converted from signed int to
size_t, which is an unsigned type, before the multiplication with
sizeof(FLOAT), and then converted to long int. Are you /sure/ this
does what you want? (It doesn't for some sizes of size_t and long).
fseek (fptr1, -(SAMPLES-STEP)*(long)sizeof(FLOAT), SEEK_CUR);
is a lot clearer in my opinion.
> for (i = 0;i < FFT_LEN; i++)
> fft_out[i] = amp(i, wave, 14);
You only have FFT_LEN/2 elements in fft_out[].
> fwrite(temp2,sizeof(float),FFT_LEN/2,fptr3);
You consistently neglect to check for write errors.
> const unsigned int reverse (unsigned int val, int bits)
The const is useless here.
> FLOAT hypot2(FLOAT a, FLOAT b)
> {
[snip]
> c = (FLOAT)sqrt((double)(a*a) + (double)(b*b));
The multiplications are (or may be) performed in float.
You probably meant:
c = (FLOAT)sqrt((double)a*a + (double)b*b);
> void fft_init (int bits)
> {
> int i;
>
> const FLOAT TWOPIoN = (atan(1.0) * 8.0) / (FLOAT)SAMPLES;
> const FLOAT TWOPIoNm1 = (atan(1.0) * 8.0) / (FLOAT)(SAMPLES - 1);
> const FLOAT TWOPISAM = 2.0*PI/(FLOAT)SAMPLES;
No declaration of PI, when compiling i standard mode. (I don't
think there's anything DJGPP-specific in your code).
> SineTable = (FLOAT *)malloc (sizeof(FLOAT) * SAMPLES);
> CosineTable = (FLOAT *)malloc (sizeof(FLOAT) * SAMPLES);
> WinTable = (FLOAT *)malloc (sizeof(FLOAT) * SAMPLES);
You don't check if the allocations succeed.
> void stuff2(complx wave[])
> {
> int i,limit,j,jj;
> complx temp[FFT_LEN];
> complx temper6[FFT_LEN];
> complx temper7[FFT_LEN];
> /*>>>>>>the next line is where it seems to die <<<<<<<<<*/
More arrays placed on the stack. You could be running out of
stack space.
> while(!feof(fptr6))
> fwrite(temper6,sizeof(float),1,fptr6);
What is this supposed to do? Run out of disc space?
> for(i = limit, j = 0; i < limit+SAMPLES, j < SAMPLES; i++,j++)
(i < limit+SAMPLES) does nothing because it is the left-hand side of
the comma operator. You probably want:
for(i = limit, j = 0; i < limit+SAMPLES && j < SAMPLES; i++,j++)
> while(!feof(fptr7))
> fwrite(temper7,sizeof(float),1,fptr7);
Another one of these...
> /* fft.h: Defs for fft routines
> * [Part of simple-fft-1.5.tar.Z (ver 1.5)]*/
>
> #include <stdio.h>
> #include <math.h>
I would include these in fft.c, where they are actually needed.
--n
- Raw text -