Home > Back-end >  Programming a rookie for help
Programming a rookie for help

Time:09-24

Try adapting a program, always has a problem, don't know what to do, a great god help guide
Procedure is as follows:
# include "fftw3. H"
#include
#include
#include
#include
#include

//const int n=4240;
//const double PI=3.1416;
//const double FN=4000;

Void FFTW (double FN, double Max_A [2], int n)
{
Output and input FILE * and * * input2;
Char inputfile []="input. TXT".//input from a file. TXT read original data
Char outputfile []="output. TXT".//will output the results to the output file. TXT in
Char input2file []="input2. TXT".
//fast Fourier transform

In fftw_complex * and * out;
Fftw_plan p;
In=(fftw_complex *) fftw_malloc (sizeof (fftw_complex) * n);
Out=(fftw_complex *) fftw_malloc (sizeof (fftw_complex) * n);

P=fftw_plan_dft_1d (n, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
Fftw_execute (p);//execute transform

///* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
//as frequency, mode and Angle of output in the form of P wave//
Max_A [0]=0.0;///frequency
Max_A [1]=0.0;//amplitude
Int countOut=0;
Fprintf (output, "frequency Model \ n");
For (int I=0; I & lt; n; I + +)
{
If (Max_A [1] {
FN Max_A [0]=I */n;
Max_A [1]=SQRT (out [I] [0] * out [I] [0] + out [I] [1] * out [I] [1]).
}
Fprintf (output, "% 9.6 f % e \ n", FN/n, I * SQRT (out [I] [0] * out [I] [0] + out [I] [1] * out [I] [1]));
CountOut++;
}
Printf (" the output is % d \ n ", countOut);
Fprintf (output, "=================================\ n");

If (fclose (input))
{
Printf (" the File close error. ");
The exit (1);
}
If (fclose (output))
{
Printf (" the File close error. ");
The exit (1);
}
Fftw_destroy_plan (p);
Fftw_free (in);
Fftw_free (out);
}

Void Trans (double FN, double Max_A [], long a pick, long n)
{
//FN: from frequency;
//pick: starting point FFT transform;
//n: the length of FFT transform
//Max_A []
//CBR
//irsm
In fftw_complex * and * out;
In=(fftw_complex *) fftw_malloc (sizeof (fftw_complex) * n);
Out=(fftw_complex *) fftw_malloc (sizeof (fftw_complex) * n);
int i;//m for the revised length FFT transform
Double tem1, tem2;
Output and input FILE * and * * input2;
Char inputfile []="input. TXT".//input from a file. TXT read original data
Char outputfile []="output. TXT".//will output the results to the output file. TXT in
Char input2file []="input2. TXT".
if (! (input=fopen (inputfile, "r")))
{
Printf (" always open a file. ");
The exit (1);
}
if (! (the output=fopen (outputfile, "a")))
{
Printf (" always open a file. ");
The exit (1);
}
if (! (input2=fopen (input2file, "a")))
{
Printf (" always open a file. ");
The exit (1);
}
For (I=0; i{
Lf lf fscanf (input, "% %", & amp; Tem1, & amp; Tem2);


}
int count=0;
for(i=0; i{
Lf lf fscanf (input, "% %", & amp; [I] in [0], & amp; [I] in [1]);
Fprintf (input2, "% 2 E % E \ n", in [I] [0], [I] in [1]).
count++;
}
Fprintf (input2, "=================================\ n");
Printf (" input is % d \ n ", count);
FFTW (FN, Max_A, n);
}

Void FFT_test (double FN, long P_Pick, long S_Pick)
{
Long P_Long S_Long;
Int count=0;
Double tem1;
Double Max_AP [2], Max_AS [2];//store the P and S largest amplitude and frequency, Max_AP [0] frequency, Max_AP [1] amplitude,

////////////////////////////////p wave Fourier transform////////////////////////
P_Long=S_Pick P_Pick + 1;//P wave sampling length, the requirement of 2 ^ k, k as an integer
Trans (FN, P_Pick P_Long Max_AP);//p wave Fourier transform

////////////////////////////////p wave Fourier transform////////////////////////


////////////////////////////////S wave Fourier transform////////////////////////
Long Num_End;

The FILE * file1.
if (! (file1=fopen (" input. TXT ", "r")))
{
Printf (" always open a file. ");
The exit (1);
}
While ((fscanf (file1, "lf % % lf", & amp; Tem1, & amp; Tem1))!=(EOF)//read the test data
{
count ++;//
}
Num_End=count;

S_Long=Num_End S_Pick + 1;
Trans (FN, S_Pick S_Long Max_AS);//S wave Fourier transform

////////////////////////////////S wave Fourier transform////////////////////////

The output FILE *;
if (! (the output=fopen (" Max_Amplitude_fre. Dat ", "w")))
{
Printf (" always open a file. ");
The exit (1);
}
Fprintf (output, "frequency Amplittude \ n");

If (Max_AS [1] & gt; Max_AP [1])
{
Fprintf (output, "% % \ n e e," Max_AS [0], Max_AS [1]).//will Max_AS [0] and Max_AS [1] the maximum amplitude and frequency output to the database
}
The else
{
Fprintf (output, "% % \ n e e," Max_AP [0], Max_AP [1]).//will Max_AP [0] and Max_AP [1] the maximum amplitude and frequency output to the database
}

If (fclose (output))
{
Printf (" the File close error. ");
The exit (1);
}

}

Int main ()
{
Double FN=4000;//sampling frequency
Long P_Pick=1997;//p wave starting point
Long S_Pick=2127;//point end of the p wave, S wave starting point

FFT_test (FN, P_Pick S_Pick);
system("pause");
return 0;
}



nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related