-
- Notifications
You must be signed in to change notification settings - Fork 359
C cleaning code #79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
C cleaning code #79
Changes from all commits
Commits
Show all changes
5 commits Select commit Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,105 +1,88 @@ | ||
| // written by Gathros. | ||
| | ||
| #include <complex.h> | ||
| #include <math.h> | ||
| | ||
| // These headers are for presentation not for the algorithm. | ||
| #include <stdlib.h> | ||
| #include <time.h> | ||
| #include <stdio.h> | ||
| | ||
| #define PI 3.1415926535897932384626 | ||
| | ||
| void cooley_tukey(double complex *X, const size_t N){ | ||
| if(N >= 2){ | ||
| // Splits the array, so the top half are the odd elements and the bottom half are the even ones. | ||
| double complex tmp [N/2]; | ||
| for(size_t i = 0; i < N/2; ++i){ | ||
| tmp[i] = X[2*i + 1]; | ||
| X[i] = X[2*i]; | ||
| } | ||
| for(size_t i = 0; i < N/2; ++i){ | ||
| X[i + N/2] = tmp[i]; | ||
| } | ||
| void cooley_tukey(double complex *X, const size_t N) { | ||
| if (N >= 2) { | ||
| double complex tmp [N / 2]; | ||
| for (size_t i = 0; i < N / 2; ++i) { | ||
| tmp[i] = X[2*i + 1]; | ||
| X[i] = X[2*i]; | ||
| } | ||
| for (size_t i = 0; i < N / 2; ++i) { | ||
| X[i + N / 2] = tmp[i]; | ||
| } | ||
| | ||
| // Recursion. | ||
| cooley_tukey(X, N/2); | ||
| cooley_tukey(X + N/2, N/2); | ||
| cooley_tukey(X, N / 2); | ||
| cooley_tukey(X + N / 2, N / 2); | ||
| | ||
| // Combine. | ||
| for(size_t i = 0; i < N/2; ++i){ | ||
| X[i + N/2] = X[i] - cexp(-2.0*I*PI*i/N)*X[i + N/2]; | ||
| X[i] -= (X[i + N/2]-X[i]); | ||
| } | ||
| } | ||
| for (size_t i = 0; i < N / 2; ++i) { | ||
| X[i + N / 2] = X[i] - cexp(-2.0 * I * PI * i / N) * X[i + N / 2]; | ||
| X[i] -= (X[i + N / 2]-X[i]); | ||
| } | ||
| } | ||
| } | ||
| | ||
| void bit_reverse(double complex *X, size_t N){ | ||
| // Bit reverses the array X[] but only if the size of the array is less then 2^32. | ||
| double complex temp; | ||
| void bit_reverse(double complex *X, size_t N) { | ||
| double complex temp; | ||
| unsigned int b; | ||
| | ||
| for(unsigned int i = 0; i < N; ++i){ | ||
| for (unsigned int i = 0; i < N; ++i) { | ||
| b = i; | ||
| b = (((b & 0xaaaaaaaa) >> 1) | ((b & 0x55555555) << 1)); | ||
| b = (((b & 0xcccccccc) >> 2) | ((b & 0x33333333) << 2)); | ||
| b = (((b & 0xf0f0f0f0) >> 4) | ((b & 0x0f0f0f0f) << 4)); | ||
| b = (((b & 0xff00ff00) >> 8) | ((b & 0x00ff00ff) << 8)); | ||
| b = ((b >> 16) | (b << 16)) >> (32 - (unsigned int) log2((double)N)); | ||
| if(b > i){ | ||
| b = ((b >> 16) | (b << 16)) >> | ||
| (32 - (unsigned int) log2((double)N)); | ||
| if (b > i) { | ||
| temp = X[b]; | ||
| X[b] = X[i]; | ||
| X[i] = temp; | ||
| } | ||
| } | ||
| } | ||
| | ||
| void iterative_cooley_tukey(double complex *X, size_t N){ | ||
| int stride; | ||
| double complex v,w; | ||
| | ||
| // Bit reverse the array. | ||
| bit_reverse(X, N); | ||
| | ||
| // Preform the butterfly on the array. | ||
| for(int i = 1; i <= log2((double)N); ++i){ | ||
| stride = pow(2, i); | ||
| w = cexp(-2.0*I*PI/stride); | ||
| for(size_t j = 0; j < N; j += stride){ | ||
| v = 1.0; | ||
| for(size_t k = 0; k < stride/2; k++){ | ||
| X[k + j + stride/2] = X[k + j] - v*X[k + j + stride/2]; | ||
| X[k + j] -= (X[k + j + stride/2] - X[k + j]); | ||
| v *= w; | ||
| } | ||
| } | ||
| } | ||
| void iterative_cooley_tukey(double complex *X, size_t N) { | ||
| bit_reverse(X, N); | ||
| | ||
| for (int i = 1; i <= log2((double)N); ++i) { | ||
| int stride = pow(2, i); | ||
| double complex w = cexp(-2.0 * I * PI / stride); | ||
| for (size_t j = 0; j < N; j += stride) { | ||
| double complex v = 1.0; | ||
| for (size_t k = 0; k < stride / 2; ++k) { | ||
| X[k + j + stride / 2] = X[k + j] - v * X[k + j + stride / 2]; | ||
| X[k + j] -= (X[k + j + stride / 2] - X[k + j]); | ||
| v *= w; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| | ||
| void approx(double complex *X, double complex *Y, size_t N){ | ||
| // This is to show that the arrays are approximate. | ||
| for(size_t i = 0; i < N; ++i){ | ||
| printf("%f\n", cabs(X[i]) - cabs(Y[i])); | ||
| } | ||
| void approx(double complex *X, double complex *Y, size_t N) { | ||
| for (size_t i = 0; i < N; ++i) { | ||
| printf("%f\n", cabs(X[i]) - cabs(Y[i])); | ||
| } | ||
| } | ||
| | ||
| int main(){ | ||
| // Initalizing the arrays for FFT. | ||
| srand(time(NULL)); | ||
| const size_t N = 64; | ||
| double complex x[N], y[N], z[N]; | ||
| for(size_t i = 0; i < N; ++i){ | ||
| x[i] = rand() / (double) RAND_MAX; | ||
| y[i] = x[i]; | ||
| z[i] = x[i]; | ||
| } | ||
| int main() { | ||
| srand(time(NULL)); | ||
| double complex x[64], y[64], z[64]; | ||
| for (size_t i = 0; i < 64; ++i) { | ||
| x[i] = rand() / (double) RAND_MAX; | ||
| y[i] = x[i]; | ||
| z[i] = x[i]; | ||
| } | ||
| | ||
| // Preform FFT. | ||
| cooley_tukey(y, N); | ||
| iterative_cooley_tukey(z, N); | ||
| cooley_tukey(y, 64); | ||
| iterative_cooley_tukey(z, 64); | ||
| | ||
| // Check if the different methods are approximate. | ||
| approx(y, z, N); | ||
| approx(y, z, 64); | ||
| | ||
| return 0; | ||
| return 0; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This
Nis the only parameter which isconst, which is quite odd.