Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 81 additions & 36 deletions 5_practice.c
Original file line number Diff line number Diff line change
@@ -1,40 +1,85 @@
#include <stdio.h> // for printf function
#include <stdlib.h> // for memory allocation
#include <time.h> // for time calculation
#include <math.h> // for sine and cosine functions
int main() {
// Declare all the variables
int k, n, N;
double *x, *yr, *yi;
time_t t;
//
// DFT
//
// Created by Liu Yuting on 2022/3/9.
//

// Input the number N
// Locate the memory for x, yr, yi;
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

// Initial setting for x, for example, x[k] = k




t = clock();
// yr[n]+i*yi[n] = sum(exp(-i 2 Pi k n / N)*x[k], k=0..N-1), n=0..N-1
int main(int argc, const char * argv[]) {
// Declare variables.
int N=7;
int n, k;
int index;

double *x, *yr, *yi, *cos_array, *sin_array;
x=(double *) malloc(N*sizeof(double));
yr=(double *) malloc(N*sizeof(double));
yi=(double *) malloc(N*sizeof(double));
cos_array=(double *) malloc(N*sizeof(double));
sin_array=(double *) malloc(N*sizeof(double));
for(n=0; n<N; n++) {
//Initialize x.
x[n]=n;
}
time_t t;

t=clock();

cos_array[0]=1.0;
sin_array[0]=0.0;
cos_array[1]=cos(2*M_PI/N);
sin_array[1]=sin(2*M_PI/N);

for(n=2; n<N; n++) {
//Compute table of cos and sin.
cos_array[n]=cos_array[1]*cos_array[n-1]-sin_array[1]*sin_array[n-1];
sin_array[n]=cos_array[1]*sin_array[n-1]+sin_array[1]*cos_array[n-1];
}

for(k=0; k<N; k++) {
//Compute DFT.
yr[k]=0.0;
yi[k]=0.0;
for(n=0; n<N; n++) {
//Compute yr[k], yi[k].
index=(n*k)%N;
yr[k]+=x[n]*cos_array[index];
yi[k]+=x[n]*sin_array[index];
}
}

t=clock()-t;

printf("x:");
for(n=0; n<N; n++) {
//Print values of x.
printf(" %f", x[n]);
}
printf("\n");

printf("yr:");
for(k=0; k<N; k++) {
//Print values of yr.
printf(" %f", yr[k]);
}
printf("\n");

printf("yi:");
for(k=0; k<N; k++) {
//Print values of yi.
printf(" %f", yi[k]);
}
printf("\n");

printf("Time: %f s\n", 1.0*t/CLOCKS_PER_SEC);

free(yi);
free(yr);
free(x);







// output the results
t = clock() - t;
printf("%d ms for discrete Fourier Transform of %d elements\n", t, N);

// free the memory located by x, yr, yi



return 100;
return 0;
}