-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_ps.c
96 lines (81 loc) · 1.67 KB
/
get_ps.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_fft_complex.h>
#include "get_ps.h"
static void fft (double *ar,int n,double flag);
#define PI 3.1415926535897932384626433832795
#define BUFLEN 512
#define REAL(z,i) ((z)[2*(i)])
#define IMAG(z,i) ((z)[2*(i)+1])
static void print_spectrum(double *, int);
static void swap(double a,double b) {
double w;
w=a; a=b; b=w;
}
void get_spectrum(double *ar, int n)
{
double *ap;
int i;
if(! ar){
return;
}
/*
fft(ar,n,0); // flag=0:FFT,-1:RFT
for (ap=ar,i=0;i<n;i++,ap++) {
*ap = sqrt(*ap * *ap)/n;
}
*/
gsl_fft_complex_radix2_forward(ar, 1, n);
for(i=0;i<n;i++){
ar[i] = sqrt(REAL(ar,i)*REAL(ar,i) + IMAG(ar,i)*IMAG(ar,i))/n;
}
}
// fftの計算
static void fft(double *ar,int n,double flg) {
int nh, nhlf;
int m0, m1;
int k0, k1;
int n0, n1;
int i,j;
int *mw;
double ark1;
// work area for bit operation
mw = (int *)malloc(n*sizeof(int));
for(i=0;i<n;i++) mw[i] = 0;
// FFT
nh = nhlf = n/2;
while(nh >= 1) {
for(n0=0;n0<n;n0+=(2*nh)) {
n1 = n0+nh;
m0 = mw[n0]/2;
m1 = m0 + nhlf;
for(k0=n0;k0<n1;k0++) {
k1 = k0+nh;
ark1 = ar[k1];
ar[k1] = ar[k0]-ark1;
ar[k0] = ar[k0]+ark1;
mw[k0] = m0;
mw[k1] = m1;
}
}
nh /= 2;
}
// responce for "flag"
if(flg<0) { // flg<0: rft
for(i=0;i<n;i++) ar[i] /= n;
}
// bit reverse operation
for(i=0;i<n;i++) {
j = mw[i];
if( j>i ) swap(ar[i],ar[j]);
}
free(mw);
return;
}