int main() { int n, ip[NMAXSQRT + 2]; double a[NMAX + 1], w[NMAX * 5 / 4], t[NMAX / 2 + 1], err; printf("data length n=? (must be 2^m)\n"); scanf("%d", &n); ip[0] = 0; /* check of CDFT */ putdata(0, n - 1, a); cdft(n, 1, a, ip, w); cdft(n, -1, a, ip, w); err = errorcheck(0, n - 1, 2.0 / n, a); printf("cdft err= %g \n", err); /* check of RDFT */ putdata(0, n - 1, a); rdft(n, 1, a, ip, w); rdft(n, -1, a, ip, w); err = errorcheck(0, n - 1, 2.0 / n, a); printf("rdft err= %g \n", err); /* check of DDCT */ putdata(0, n - 1, a); ddct(n, 1, a, ip, w); ddct(n, -1, a, ip, w); a[0] *= 0.5; err = errorcheck(0, n - 1, 2.0 / n, a); printf("ddct err= %g \n", err); /* check of DDST */ putdata(0, n - 1, a); ddst(n, 1, a, ip, w); ddst(n, -1, a, ip, w); a[0] *= 0.5; err = errorcheck(0, n - 1, 2.0 / n, a); printf("ddst err= %g \n", err); /* check of DFCT */ putdata(0, n, a); a[0] *= 0.5; a[n] *= 0.5; dfct(n, a, t, ip, w); a[0] *= 0.5; a[n] *= 0.5; dfct(n, a, t, ip, w); err = errorcheck(0, n, 2.0 / n, a); printf("dfct err= %g \n", err); /* check of DFST */ putdata(1, n - 1, a); dfst(n, a, t, ip, w); dfst(n, a, t, ip, w); err = errorcheck(1, n - 1, 2.0 / n, a); printf("dfst err= %g \n", err); return 0; }
inline double dcdflib_t_quantile(double p, double param) { int what = 2; int status = 0; double x, bound, q(1 - p); cdft(&what, &p, &q, &x, ¶m, &status, &bound); return x; }
inline double dcdflib_t_cdf(double x, double param) { int what = 1; int status = 0; double p, q, bound; cdft(&what, &p, &q, &x, ¶m, &status, &bound); return p; }
void V_cdft(int *which, double *p, double *q, double *t, double *df, int *status, double *bound, int *len) { int i; for (i = 0; i < *len; i++) { cdft((int *)which, &p[i], &q[i], &t[i], &df[i], (int *)&status[i], &bound[i]); } }
static int stat_pt (lua_State *L) { /* stack should contain t and df */ lua_Number t = luaL_checknumber(L, 1); lua_Number df = luaL_checknumber(L, 2); lua_Number p, q, bound; int which = 1; int status; check_t(L, 1, t, df); cdft(&which, &p, &q, &t, &df, &status, &bound); check_status(L, status, bound); lua_pushnumber(L, p); return 1; }
/** * Applies the inverse transform to the spectrum. * * @param spectrum input spectrum * @param x output signal */ void OouraFft::ifft(ComplexType spectrum[], double x[]) { // We assume here that a complex<double> has the same // representation in memory as two consecutive doubles BOOST_STATIC_ASSERT(sizeof(ComplexType[2]) == sizeof(double[4])); double* a = reinterpret_cast<double*>(spectrum); // Ooura's function cdft(2*N, 1, a, ip, w); // copy the data to the double array and scale it for (size_t i = 0; i < N; ++i) { x[i] = a[2*i] / static_cast<double>(N); } }
/** * Applies the transformation to the signal. * * @param x input signal * @param spectrum output spectrum */ void OouraFft::fft(const SampleType x[], ComplexType spectrum[]) { // We assume here that a complex<double> has the same // representation in memory as two consecutive doubles BOOST_STATIC_ASSERT(sizeof(ComplexType[2]) == sizeof(double[4])); double* a = reinterpret_cast<double*>(spectrum); // copying input data to even elements of the array (real values) for (size_t i = 0; i < N; ++i) { a[2*i] = x[i]; } // let's call the C function from Ooura's package cdft(2*N, -1, a, ip, w); }
static int cdf_qt (lua_State *L) { /* stack should contain p and df */ lua_Number p = luaL_checknumber(L, 1); lua_Number df = luaL_checknumber(L, 2); lua_Number t; check_t(L, 2, p, df); if (p==0 || p==1) t = (p==0) ? -HUGE_VAL : HUGE_VAL; else { lua_Number q = 1-p; lua_Number bound; int which = 2; int status; cdft(&which, &p, &q, &t, &df, &status, &bound); check_status(status, bound); } lua_pushnumber(L, t); return 1; }
int main() { FILE *fp_r = fopen("Eth_r.txt", "r"); FILE *fp_i = fopen("Eth_i.txt", "r"); Complex datas[n]; memset(datas, 0, sizeof(Complex)*n); for(int j=0; j<1500; j++) { double rm, im; fscanf(fp_r, "%lf", &rm); fscanf(fp_i, "%lf", &im); datas[j] = rm + I*im; } printf("read file\n"); FILE *outR = fopen("re.txt", "w"); FILE *outI = fopen("im.txt", "w"); for(int i=0; i<1500; i++){ fprintf(outR, "%lf\n", creal(datas[i])); fprintf(outI, "%lf\n", cimag(datas[i])); } fclose(outR); fclose(outI); Complex *res = cdft(datas, n); FILE *fp = fopen("rest2.txt", "w"); for(int i=0; i<1500; i++) { fprintf(fp, "%lf\n", cabs(res[i]) / sqrt(n)); } free(res); fclose(fp); printf("finish\n"); return 0; }
void CFSKModem::SmoothingWave(short *InDataBuf,unsigned long length, unsigned long LowF, unsigned long HighF, unsigned long SampleRate) { unsigned long i, j, k, N; long start, end; unsigned long l, h; /* unsigned long NumberOfLow = 0;//小幅度波的个数 for(;i<length;) { NumberOfLow = 0; while((InDataBuf[i] < 500)&&(InDataBuf[i] > -500)) //去除 连续 3点小于500的点 { i++; NumberOfLow++; if(i == length) { return; } } if(NumberOfLow < 3)//对于中间出现的小幅度波小于3,要退回去 { i -= NumberOfLow; } //InDataBuf[i] = (InDataBuf[i - 1] + InDataBuf[i] + InDataBuf[i + 1])/3; //i++; }*/ #if 0 for(i = 0; i < length;) { FindFrame(InDataBuf + i, length - i, &start, &end); if(start >= 0) { if(end == -1) end = length - i - 1; N = GetN(end - start + 512); // 前后各至少填充256点,每点值为0 memset(this->m_fPoint, 0, 2 * N * sizeof(float)); for (j = 256; j < N; j ++) { this->m_fPoint[2*j] = InDataBuf[i+start+j-256]; } /* Calculate FFT. */ cdft(N*2, -1, this->m_fPoint); /* Filter */ l = (unsigned long)(LowF/((float)SampleRate/N)); h = (unsigned long)(HighF/((float)SampleRate/N)); for(k = 0; k < l; k ++) { this->m_fPoint[2*k] = this->m_fPoint[2*k+1] = 0; } for(k = h; k < N; k ++) { this->m_fPoint[2*k] = this->m_fPoint[2*k+1] = 0; } /* Clear time-domain samples and calculate IFFT. */ memset(InDataBuf+i+start, 0, (end-start)*2); icdft(N*2, -1, this->m_fPoint); for(k = 0; k < end-start; k ++) { InDataBuf[i+start+k] = (short)this->m_fPoint[2*k+256]; } i += end; } else break; } #else for(i = 0; i < length;) { this->FindFrame(InDataBuf + i, length - i, &start, &end); if(start >= 0) { if(end == -1) end = length - i - 1; N = this->GetN(end - start + 512); // 前后各至少填充256点,每点值为0 memset(this->m_fPoint, 0, 2 * N * sizeof(float)); for (j = 256; j < end - start + 256; j ++) { this->m_fPoint[j] = InDataBuf[i+start+j-256]; } /* Calculate FFT. */ rdft(N, 1, this->m_fPoint); /* Filter */ l = (unsigned long)(LowF/((float)SampleRate/N)); h = (unsigned long)(HighF/((float)SampleRate/N)); for(k = 0; k < l; k ++) { this->m_fPoint[2*k] = this->m_fPoint[2*k+1] = 0; } for(k = h; k < N; k ++) { this->m_fPoint[2*k] = this->m_fPoint[2*k+1] = 0; } /* Clear time-domain samples and calculate IFFT. */ memset(InDataBuf+i+start, 0, (end-start)*2); rdft(N, -1, this->m_fPoint); for (j = 0; j <= N - 1; j++) { //this->m_fPoint[j] *= 2.0/ N; this->m_fPoint[j] /= N; } for(k = 0; k < end-start; k ++) { InDataBuf[i+start+k] = (short)this->m_fPoint[k+256]; } i += end; } else break; } #endif }
void CFSKModem::SmoothingWave(short *InDataBuf,unsigned long length, unsigned long LowF, unsigned long HighF, unsigned long SampleRate) { unsigned long i, j, k, N; long start, end; unsigned long l, h; #if 0 for(i = 0; i < length;) { FindFrame(InDataBuf + i, length - i, &start, &end); if(start >= 0) { if(end == -1) end = length - i - 1; N = GetN(end - start + 512); // 前后各至少填充256点,每点值为0 memset(this->m_fPoint, 0, 2 * N * sizeof(float)); for (j = 256; j < N; j ++) { this->m_fPoint[2*j] = InDataBuf[i+start+j-256]; } /* Calculate FFT. */ cdft(N*2, -1, this->m_fPoint); /* Filter */ l = (unsigned long)(LowF/((float)SampleRate/N)); h = (unsigned long)(HighF/((float)SampleRate/N)); for(k = 0; k < l; k ++) { this->m_fPoint[2*k] = this->m_fPoint[2*k+1] = 0; } for(k = h; k < N; k ++) { this->m_fPoint[2*k] = this->m_fPoint[2*k+1] = 0; } /* Clear time-domain samples and calculate IFFT. */ memset(InDataBuf+i+start, 0, (end-start)*2); icdft(N*2, -1, this->m_fPoint); for(k = 0; k < end-start; k ++) { InDataBuf[i+start+k] = (short)this->m_fPoint[2*k+256]; } i += end; } else break; } #else for(i = 0; i < length;) { this->FindFrame(InDataBuf + i, length - i, &start, &end); if(start >= 0) { if(end == -1) end = length - i - 1; N = this->GetN(end - start + 512); // 前后各至少填充256点,每点值为0 memset(this->m_fPoint, 0, 2 * N * sizeof(float)); for (j = 256; j < end - start + 256; j ++) { this->m_fPoint[j] = InDataBuf[i+start+j-256]; } /* Calculate FFT. */ rdft(N, 1, this->m_fPoint); /* Filter */ l = (unsigned long)(LowF/((float)SampleRate/N)); h = (unsigned long)(HighF/((float)SampleRate/N)); for(k = 0; k < l; k ++) { this->m_fPoint[2*k] = this->m_fPoint[2*k+1] = 0; } for(k = h; k < N; k ++) { this->m_fPoint[2*k] = this->m_fPoint[2*k+1] = 0; } /* Clear time-domain samples and calculate IFFT. */ memset(InDataBuf+i+start, 0, (end-start)*2); rdft(N, -1, this->m_fPoint); for (j = 0; j <= N - 1; j++) { //this->m_fPoint[j] *= 2.0/ N; this->m_fPoint[j] /= N; } for(k = 0; k < end-start; k ++) { InDataBuf[i+start+k] = (short)this->m_fPoint[k+256]; } i += end; } else break; } #endif }
void SmoothingWave(short *InDataBuf,unsigned long length, unsigned long LowF, unsigned long HighF, unsigned long SampleRate) { unsigned long i, j, k, N; long start, end; unsigned long l, h; float *g_fPoint; /* pointer to time-domain samples */// 这个东西至今没有确切的了解是什么,先放着 g_fPoint = (float*)malloc(2 * MAX_N_POINTS * sizeof(float));//滤波的时候保存数据 #if 0 for(i = 0; i < length;) { FindFrame(InDataBuf + i, length - i, &start, &end); if(start >= 0) { if(end == -1) end = length - i - 1; N = GetN(end - start + 512); // 前后各至少填充256点,每点值为0 memset(m_fPoint, 0, 2 * N * sizeof(float)); for (j = 256; j < N; j ++) { m_fPoint[2*j] = InDataBuf[i+start+j-256]; } /* Calculate FFT. */ cdft(N*2, -1, m_fPoint); /* Filter */ l = (unsigned long)(LowF/((float)SampleRate/N)); h = (unsigned long)(HighF/((float)SampleRate/N)); for(k = 0; k < l; k ++) { m_fPoint[2*k] = m_fPoint[2*k+1] = 0; } for(k = h; k < N; k ++) { m_fPoint[2*k] = m_fPoint[2*k+1] = 0; } /* Clear time-domain samples and calculate IFFT. */ memset(InDataBuf+i+start, 0, (end-start)*2); icdft(N*2, -1, m_fPoint); for(k = 0; k < end-start; k ++) { InDataBuf[i+start+k] = (short)m_fPoint[2*k+256]; } i += end; } else break; } #else for(i = 0; i < length;) { FindFrame(InDataBuf + i, length - i, &start, &end); if(start >= 0) { if(end == -1) end = length - i - 1; N = GetN(end - start + 512); // 前后各至少填充256点,每点值为0 memset(g_fPoint, 0, 2 * N * sizeof(float)); for (j = 256; j < end - start + 256; j ++) { g_fPoint[j] = InDataBuf[i+start+j-256]; } /* Calculate FFT. */ rdft(N, 1, g_fPoint); /* Filter */ l = (unsigned long)(LowF/((float)SampleRate/N)); h = (unsigned long)(HighF/((float)SampleRate/N)); for(k = 0; k < l; k ++) { g_fPoint[2*k] = g_fPoint[2*k+1] = 0; } for(k = h; k < N; k ++) { g_fPoint[2*k] = g_fPoint[2*k+1] = 0; } /* Clear time-domain samples and calculate IFFT. */ memset(InDataBuf+i+start, 0, (end-start)*2); rdft(N, -1, g_fPoint); for (j = 0; j <= N - 1; j++) { //m_fPoint[j] *= 2.0/ N; g_fPoint[j] /= N; } for(k = 0; k < end-start; k ++) { InDataBuf[i+start+k] = (short)g_fPoint[k+256]; } i += end; } else { free(g_fPoint); break; } } free(g_fPoint); #endif }