diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..2b6a312 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,21 @@ +{ + "configurations": [ + { + "name": "Win32", + "includePath": [ + "${workspaceFolder}/**" + ], + "defines": [ + "_DEBUG", + "UNICODE", + "_UNICODE" + ], + "windowsSdkVersion": "10.0.19041.0", + "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe", + "cStandard": "c17", + "cppStandard": "c++17", + "intelliSenseMode": "windows-msvc-x64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/5_practice.c b/5_practice.c index 70d6651..9f36ffc 100644 --- a/5_practice.c +++ b/5_practice.c @@ -9,32 +9,43 @@ int main() { time_t t; // Input the number N - + printf("Enter N = "); + scanf("%d",&N); // Locate the memory for x, yr, yi; - - // Initial setting for x, for example, x[k] = k - - + x = (double *) malloc(N*sizeof(double)); + yr = (double *) malloc(N*sizeof(double)); + yi = (double *) malloc(N*sizeof(double)); + // Initial setting for x, for example, x[k] = k + for(int i = 0; i +#include +#include +#include "complex.h" + +int roundup(int a, int b){ + int c; + if(a%b){ + a += (b-a%b); + } + c = a/b; + return c; +} + +complex FFTP(complex *y, complex *x, int N, int p){ + + //declare some variables + int i, j, k, Np = N/p; //i,j,k for index using + + //prime factorization + //delcare some variables + int nextp, nextN, psize; + int *prime, *ptime; //record prime and square number + int Ntmp = N, index = 0, t, ctrl, final; + + //memory allocation for prime and its square number + psize = roundup(N,2); + prime = (int *) malloc( psize * sizeof(int)); + ptime = (int *) malloc( psize * sizeof(int)); + + for(i = 2; i < psize; i++){ + t = 0; + while(Ntmp%i == 0){ + Ntmp /= i; + t++; //sum up square number + ctrl = 1; //switch the position of the array + } + + if(ctrl == 1){ + prime[index] = i; //prime + ptime[index] = t; //square number of the prime resp. + index++; + } + ctrl = 0; + } + if(!index){ + nextp = N; + final = 1; //as N = p, do the final FFT + prime[index] = N; + ptime[index] = 1; + }else if(ptime[index-1] > 1){ + nextp = prime[index-1]; + }else{ + nextp = prime[index-2]; + } + + //FFTP + if(Np == 1 || final){ + p = N; + complex *w; + w = (complex *) malloc( p * sizeof(complex) ); + for(i = 0; i < p; i++){ + w[i] = wtrans(N,i); + } + for(i = 0; i < p; i++){ + y[i].Re = 0; + y[i].Im = 0; + for(j = 0; j < p; j++){ + y[i] = Cadd(y[i], Cmulti( x[j] ,w[(i*j)%N] ) ); + } + } + free(w); + }else{ + + //memory allocation for subseq + nextN = N / nextp; + complex **xsub, **ysub; + xsub = (complex **) malloc( nextp * sizeof(complex) ); + ysub = (complex **) malloc( nextp * sizeof(complex) ); + for(i = 0; i < nextp; i++){ + xsub[i] = (complex *) malloc( nextN * sizeof(complex) ); + ysub[i] = (complex *) malloc( nextN * sizeof(complex) ); + } + + //assign x subseq and y + for(i = 0; i < nextN; i++){ + for(j = 0; j < nextp; j++){ + xsub[i][j] = x[nextp*j + i]; + y[i*nextN + j].Re = 0; + y[i*nextN + j].Im = 0; + } + } + + //do the subseq FFT + for(i = 0;i < nextp; i++){ + FFTP( *(ysub+i), *(xsub+i), nextN, nextp); + } + + //combine the subseq + complex *w; + w = (complex *) malloc( nextp * sizeof(complex)); + for( j = 0; j < nextp; j++){ + w[j] = wtrans(N,i); + } + for(i = 0; i < nextp; i++){ + for( j = 0; j < nextN; j++){ + for(k = 0; k < nextp; k++){ + y[ nextp * i + j ] = Cadd( y[ nextp * i + j ] , \ + Cmulti(ysub[k][j], w[ ( k* (i*N/p+j) ) %N] ) ); + } + } + } + //free every memory + free(w); + for(i = 0; i < nextp; i++){ + free(xsub[i]); + free(ysub[i]); + } + free(xsub);free(ysub); + + } + + free(prime);free(ptime); + + //end + complex c; + c.Re = 0; + c.Im = 0; + return c; +} diff --git a/FFT_235.c b/FFT_235.c new file mode 100644 index 0000000..f673901 --- /dev/null +++ b/FFT_235.c @@ -0,0 +1,103 @@ +#include +#include +#include +#include +#include "fft.h" + +//declare the function FFT +int FFT(double *yr, double *yi, double *xr, double *xi, int N); + +int main(){ + + //declare variables + //Suppose N is the total number, pqr are square of 235 respec. + //Suppose + int k, n, N, p, q, r, ctrl; + double *xr, *xi, *yr, *yi, t, a, b, an, bn, c, s; + time_t T; + + xr = (double *) malloc(N*sizeof(double)); + xi = (double *) malloc(N*sizeof(double)); + yr = (double *) malloc(N*sizeof(double)); + yi = (double *) malloc(N*sizeof(double)); + + printf("Memory Done! \n"); + + for(k = 0;k<5;++k){ + printf("%d: %f \n",k,xr[k]); + } + + T = clock(); + + a = cos(2 * M_PI / N); b = sin(2 * M_PI / N); + an = 1; bn = 0; + for(n = 0; n < N; n++){ + yr[n] = 0.0; + yi[n] = 0.0; + c = 1; s = 0; + for(k = 0; k +#include +#include +#include + +int FFT(double *yr, double *yi, double *xr, double *xi, int N); + +int main(){ + int k,n,N; + double *xr, *xi, *yr, *yi, *zr, *zi, *ur, *ui, t, a, b, an, bn, c, s; + time_t T; + + FILE *fp; + char ch; + int L = 0; + fp = fopen("wave.csv","r"); + while(!feof(fp)){ + ch = fgetc(fp); + if(ch=='\n'){ + L++; + } + } + fclose(fp); + printf("Total Lines: %d \n",L); + + N = 1; + while(Nt){ + n = k; + t = sqrt(zr[k]*zr[k]+zi[k]*zi[k]); + } + } + printf("mazimum at %d, value = %f\n", n, t/N*2); + printf("BPM %f\n",60.0*n/(N/240)); //240 = Sample Rate + + free(xr);free(xi);free(yr);free(yi); + + return 0; + +} + +int FFT(double *yr, double *yi, double *xr, double *xi, int N){ + if(N == 2){ + yr[0] = xr[0]+xr[1]; + yi[0] = xi[0]+xi[1]; + yr[1] = xr[0]-xr[1]; + yr[1] = xi[0]-xi[1]; + } else { + int k; + double *yEr, *yEi, *yOr, *yOi; + double *xEr, *xEi, *xOr, *xOi; + double c, s; + + xEr = (double *) malloc((N/2)*sizeof(double)); + xEi = (double *) malloc((N/2)*sizeof(double)); + yEr = (double *) malloc((N/2)*sizeof(double)); + yEi = (double *) malloc((N/2)*sizeof(double)); + xOr = (double *) malloc((N/2)*sizeof(double)); + xOi = (double *) malloc((N/2)*sizeof(double)); + yEr = (double *) malloc((N/2)*sizeof(double)); + yOi = (double *) malloc((N/2)*sizeof(double)); + for(k = 0; k +#include +#include +#include + +int roundup(int a, int b){ + int c; + if(a%b){ + a += (b-a%b); + } + c = a/b; + return c; +} + +complex FFT(complex *y, complex *x, int N){ + //declare some variables + int i, j, k, tmp; //i,j,k for index using + + //prime factorization + //delcare some variables + int nextp, nextN, psize; + int *prime, *ptime; //record prime and square number + int Ntmp = N, index = 0, t, ctrl, final=0; + + //memory allocation for prime and its square number + psize = roundup(N,2); + prime = (int *) malloc( psize * sizeof(int)); + ptime = (int *) malloc( psize * sizeof(int)); + + for(i = 2; i < N; i++){ + t = 0; + while(Ntmp%i == 0){ + Ntmp /= i; + t++; //sum up square number + ctrl = 1; //switch the position of the array + } + + if(ctrl == 1){ + prime[index] = i; //prime + ptime[index] = t; //square number of the prime resp. + index++; + } + ctrl = 0; + } + prime[index] = N; + ptime[index] = 1; + + if((prime[0] == N && ptime[0] == 1)||(!index)){ //as N = p, do the final FFT + nextp = N; + final = 1; + //printf("1. N is %d, nextp is %d \n",N,nextp); + }else if(ptime[index-1] >= 1){ + nextp = prime[index-1]; + //printf("2. N is %d, nextp is %d \n",N,nextp); + }else{ + nextp = prime[index-2]; + } + + //FFT + complex wN = cexp(-2*M_PI*I/N); + + complex *w; + w = (complex *) malloc( N * sizeof(complex) ); + for( j = 0; j < N; j++){ + w[j] = cpow(wN,j); + } + if(final){ //final subseq, p = N + for(i = 0; i < N; i++){ + for(j = 0; j < N; j++){ + y[i] += x[j] * w[(i*j)%N]; + } + } + } + else{ + + complex *xsub, *ysub; + xsub = (complex *) malloc( N * sizeof(complex) ); + ysub = (complex *) malloc( N * sizeof(complex) ); + + //memory allocation for subseq + nextN = N/nextp; + + //assign x subseq and set y zero seq + for(i = 0; i < nextp; i++){ + tmp = nextN*i; + for(j = 0; j < nextN; j++){ + index = j + tmp; + xsub[index] = x[nextp*j + i]; + ysub[index] = 0; + } + } + tmp = 0;index = 0; + + //do the subseq FFT + for(i = 0; i < nextp; i++){ + FFT( ysub+i*nextN, xsub+i*nextN, nextN); + } + + for(i = 0; i < nextp; i++){ //in how many parts + tmp = nextN*i; + for( j = 0; j < nextN; j++){ //seq index + index = tmp + j; + for(k = 0; k < nextp; k++){ //subseq index + y[index] += ysub[nextN*k+j] * w[(k*index)%N]; + } + } + } + index = 0; + free(ysub); + free(xsub); + } + free(w); + free(ptime); + free(prime); + + return 0; +} + +int max_prime(int N){ + int i, p = 1, ctrl = 0, Ntmp = N; + for(i = 2; i < roundup(N,2); i++){ + while( Ntmp%i == 0){ + Ntmp /= i; + ctrl = 1; + p = i; + } + } + if(ctrl == 0){ + p = N; + } + return p; +} \ No newline at end of file diff --git a/fft_shu.cpp b/fft_shu.cpp new file mode 100644 index 0000000..5648528 --- /dev/null +++ b/fft_shu.cpp @@ -0,0 +1,150 @@ +#include +#include +#include +#include + +int FFT(double *yr, double *yi, double *xr, double *xi, int N); + +int main(){ + int k,n,N; + double *xr, *xi, *yr, *yi, *zr, *zi, *ur, *ui, t, a, b, an, bn, c, s; + time_t T; + + FILE *fp; + char ch; + int L = 0; + fp = fopen("wave.csv","r"); + while(!feof(fp)){ + ch = fgetc(fp); + if(ch=='\n'){ + L++; + } + } + fclose(fp); + printf("Total Lines: %d \n",L); + + N = 1; + while(Nt){ + n = k; + t = sqrt(zr[k]*zr[k]+zi[k]*zi[k]); + } + } + printf("mazimum at %d, value = %f\n", n, t/N*2); + printf("BPM %f\n",60.0*n/(N/240)); //240 = Sample Rate + + free(xr);free(xi);free(yr);free(yi); + + return 0; + +} + +int FFT(double *yr, double *yi, double *xr, double *xi, int N){ + if(N == 2){ + yr[0] = xr[0]+xr[1]; + yi[0] = xi[0]+xi[1]; + yr[1] = xr[0]-xr[1]; + yr[1] = xi[0]-xi[1]; + } else { + int k; + double *yEr, *yEi, *yOr, *yOi; + double *xEr, *xEi, *xOr, *xOi; + double c, s; + + xEr = (double *) malloc((N/2)*sizeof(double)); + xEi = (double *) malloc((N/2)*sizeof(double)); + yEr = (double *) malloc((N/2)*sizeof(double)); + yEi = (double *) malloc((N/2)*sizeof(double)); + xOr = (double *) malloc((N/2)*sizeof(double)); + xOi = (double *) malloc((N/2)*sizeof(double)); + yEr = (double *) malloc((N/2)*sizeof(double)); + yOi = (double *) malloc((N/2)*sizeof(double)); + for(k = 0; k +#include +#include + +void p_fac(int N, int &p, int &q, int &r); + +int main(){ + int N = 10800000, p = 0, q = 0, r = 0; + + p_fac(N, p, q, r); + + printf("p = %d, q = %d, r = %d\n", p, q, r); + + return 0; +} + +void p_fac(int N, int &p, int &q, int &r){ + while(N%5==0){ + r++; + N /= 5; + } + while(N%3==0){ + q++; + N /= 3; + } + while(N%2==0){ + p++; + N /= 2; + } +} \ No newline at end of file diff --git a/prime_fac.h b/prime_fac.h new file mode 100644 index 0000000..563629a --- /dev/null +++ b/prime_fac.h @@ -0,0 +1,33 @@ +#include +#include + +void p_fac(int N, int &p, int &q, int &r, int &ctrl){ + int O = N; + + while(N%5==0){ + r++; + N /= 5; + } + while(N%3==0){ + q++; + N /= 3; + } + while(N%2==0){ + p++; + N /= 2; + } + + if( ( p==0 & q==0 & r==0 ) || N!=1 ){ + ctrl = 1; + printf("The number %d doesn't have prime factorization in 2,3,5\n",O); + } +} + +int roundup(int a, int b){ + int c; + if(a%b){ + a += (b-a%b); + } + c = a/b; + return c; +} diff --git a/quick_sort.c b/quick_sort.c new file mode 100644 index 0000000..a338359 --- /dev/null +++ b/quick_sort.c @@ -0,0 +1,95 @@ +#include // for printf function +#include // for memory allocation +#include // for time calculation +#include // for sine and cosine functions +int main() { + // Declare all the variables + int k, m, n, N; + double *x, *y, z, p; + time_t t; + + // Input the number N + printf("Input N: "); + scanf("%d",&N); + + // Locate the memory for x and y; + x = (double *) malloc(N*sizeof(double)); + y = (double *) malloc(N*sizeof(double)); + + // Initial setting for x, for example, x[k] = 1.0*rand()/RAND_MAX + srand( time(NULL) ); + for(k=0;k x[k]) { + z = x[n]; + x[n] = x[k]; + x[k] = z; + } + } + } + t = clock() - t; + + // print y, x, and time + printf("Sorting %d elements: %f s\n", N, 1.0*t/CLOCKS_PER_SEC); + if(N<10) { + printf("y \t\t x\n"); + for(k=0;k +#include "fft.h" + +int main(){ + int k, n, v, i; + int L = 1<<20; + complex *x, *y; + + x = (complex *) malloc( L * sizeof(complex) ); + y = (complex *) malloc( L * sizeof(complex) ); + + for(k = 0;k +#include +#include +#include "complex.h" + +int roundup(int a, int b){ + int c; + if(a%b){ + a += (b-a%b); + } + c = a/b; + return c; +} + +int main(){ + int i, j, k, tmp, N = 2; //i,j,k for index using + + //prime factorization + //delcare some variables + int nextp, nextN, psize; + int *prime, *ptime; //record prime and square number + int Ntmp = N, index = 0, t, ctrl, final; + + //memory allocation for prime and its square number + psize = roundup(N,2); + prime = (int *) malloc( psize * sizeof(int)); + ptime = (int *) malloc( psize * sizeof(int)); + + for(i = 2; i <= N; i++){ + t = 0; + while(Ntmp%i == 0){ + Ntmp /= i; + t++; //sum up square number + ctrl = 1; //switch the position of the array + } + + if(ctrl == 1){ + prime[index] = i; //prime + ptime[index] = t; //square number of the prime resp. + index++; + } + ctrl = 0; + } + prime[index] = N; + ptime[index] = 1; + printf("prime done\n"); + if((prime[0] == N && ptime[0] == 1)||(!index)){ //as N = p, do the final FFT + printf("one time\n"); + final = 1; + }else if(ptime[index-1] > 1){ + printf("two time\n"); + nextp = prime[index-1]; + }else{ + printf("three time\n"); + nextp = prime[index-2]; + } +} \ No newline at end of file diff --git a/trubleshoot.cpp b/trubleshoot.cpp new file mode 100644 index 0000000..60d7be1 --- /dev/null +++ b/trubleshoot.cpp @@ -0,0 +1,13 @@ +#include +#include +#include + +int main(){ + int n = 10, m = 2,k; + + for(k = 2; k