arrays - How to use malloc in a c function? -
i want make c function fir filter, has 2 input arrays , 1 output array. both input arrays constant numbers, want use them computation of output of filter,and after computation delete them , store output array of function code not work
#include <stdlib.h> float * filter(float *patientsignal,float *filtercoef, int lengthofpatient , int lengthoffilter ){ static float firout[8000]; int i,j; float temp=0; float* signal; float* coef; signal = malloc(lengthofpatient *sizeof(float)); coef = malloc(lengthoffilter*sizeof(float)); } (j = 0; j <= lengthofpatient; j++){ temp = signal[j] * coef[0]; (i = 1; <= lengthoffilter; i++){ if ((j - i) >= 0){ temp += signal[j - i] * coef[i]; } firout[j] = temp; } } free(signal); free(coef); free(patientsignal); return firout; }
there several problems in code,
unnecessary
}
after linecoef = malloc(lengthoffilter*sizeof(float));
.for (j = 0; j <= lengthofpatient; j++)
. loop once more required. same loop. pmg mentioned in comment.temp += signal[j - i] * coef[i];
not give desired outcome, not initialized bothsignal
orcoef
.whats purpose of
float *patientsignal,float *filtercoef
in function parameter?
from wild guess, think need 2 line initialize signal
and/or coef
.
memccpy(signal, patientsignal, lengthofpatient); memccpy(coef, filtercoef, lengthoffilter);
don't free
patientsignal
in local function. let done function caller.
Comments
Post a Comment