試著改編了一段程式,老是有問題,不知道怎么辦,求大神幫忙指導
程式如下:
#include "fftw3.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <cstdlib>
#include <TCHAR.h>
//const int n=4240 ;
//const double PI = 3.1416;
//const double FN =4000;
void FFTW (double FN,double Max_A[2],int n)
{
FILE *input, *output, *input2;
char inputfile [] = "input.txt"; // 從檔案 input.txt 中讀入原始資料
char outputfile [] = "output.txt"; // 將結果輸出到檔案 output.txt 中
char input2file[] = "input2.txt";
// 快速傅立葉變換
fftw_complex *in, *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); // 執行變換
///****************************************************************************//
//以頻率、模和幅角的形式輸出P波//
Max_A[0]=0.0;///頻率
Max_A[1]=0.0;//振幅
int countOut = 0;
fprintf (output, "frequency Model \n");
for ( int i = 0; i < n; i ++)
{
if(Max_A[1]<sqrt(out[i][0]*out[i][0] + out[i][1]*out[i][1]))
{
Max_A[0]=i*FN/n;
Max_A[1]=sqrt(out[i][0]*out[i][0] + out[i][1]*out[i][1]);
}
fprintf (output, "%9.6f %e\n", i*FN/n, sqrt(out[i][0]*out[i][0] + out[i][1]*out[i][1]));
countOut++;
}
printf("output is %d\n", countOut);
fprintf (output, "================================= \n");
if ( fclose (input))
{
printf ("File close error. ");
exit (1);
}
if ( fclose (output))
{
printf ("File close error. ");
exit (1);
}
fftw_destroy_plan(p);
fftw_free(in);
fftw_free(out);
}
void Trans(double FN,double Max_A[], long pick,long n)
{
//FN:Sampling frequency;
//pick:fft變換的開始點;
//n:fft變換的長度
//Max_A[]
//cbr
//irsm
fftw_complex *in, *out;
in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * n);
out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * n);
int i;//m為修正后的fft變換長度
double tem1, tem2;
FILE *input, *output, *input2;
char inputfile [] = "input.txt"; // 從檔案 input.txt 中讀入原始資料
char outputfile [] = "output.txt"; // 將結果輸出到檔案 output.txt 中
char input2file[] = "input2.txt";
if (!(input = fopen (inputfile, "r")))
{
printf ("Cannot open file. ");
exit (1);
}
if (!(output = fopen (outputfile, "a")))
{
printf ("Cannot open file. ");
exit (1);
}
if (!(input2 = fopen(input2file, "a")))
{
printf("Cannot open file. ");
exit(1);
}
for ( i=0;i<pick-1;i++)//拋棄前pick個
{
fscanf (input, "%lf %lf", &tem1, &tem2);
}
int count = 0;
for(i=0;i< n;i++)
{
fscanf (input, "%lf %lf", &in[i][0], &in[i][1]);
fprintf(input2, "%.2E %E\n",in[i][0],in[i][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];//存盤 P and S 最大振幅和頻率,Max_AP[0]頻率,Max_AP[1]振幅,
////////////////////////////////p波傅立葉變換////////////////////////
P_Long=S_Pick-P_Pick+1;//P波采樣長度, 要求為 2^k, k為整數
Trans(FN,P_Pick,P_Long,Max_AP);//p波傅立葉變換
////////////////////////////////p波傅立葉變換////////////////////////
////////////////////////////////S波傅立葉變換////////////////////////
long Num_End;
FILE *file1;
if (!(file1 = fopen ("input.txt", "r")))
{
printf ("Cannot open file. ");
exit (1);
}
while ((fscanf (file1, "%lf %lf", &tem1, &tem1)) != EOF)//讀取試驗資料
{
count ++;//
}
Num_End=count;
S_Long=Num_End-S_Pick+1;
Trans(FN,S_Pick,S_Long,Max_AS);//S波傅立葉變換
////////////////////////////////S波傅立葉變換////////////////////////
FILE *output;
if (!(output = fopen ("Max_Amplitude_fre.dat", "w")))
{
printf ("Cannot open file. ");
exit (1);
}
fprintf (output, "frequency Amplittude \n");
if(Max_AS[1]>Max_AP[1])
{
fprintf (output, "%e %e\n",Max_AS[0],Max_AS[1]);//將Max_AS[0]和Max_AS[1]最大振幅和頻率輸出到資料庫
}
else
{
fprintf (output, "%e %e\n",Max_AP[0],Max_AP[1]);//將Max_AP[0]和Max_AP[1]最大振幅和頻率輸出到資料庫
}
if ( fclose (output))
{
printf ("File close error. ");
exit (1);
}
}
int main ()
{
double FN=4000;//采樣頻率
long P_Pick=1997;//p wave 開始點
long S_Pick=2127;//p wave 結束點,S Wave開始點
FFT_test (FN,P_Pick,S_Pick);
system("pause");
return 0;
}
生成方案時產生的問題如下:
1>------ 已啟動全部重新生成: 專案: fftw_p_s0, 配置: Debug Win32 ------
1>生成啟動時間為 2016-8-6 20:52:09。
1>_PrepareForClean:
1> 正在洗掉檔案“Debug\fftw_p_s0.lastbuildstate”。
1>InitializeBuildStatus:
1> 正在對“Debug\fftw_p_s0.unsuccessfulbuild”執行 Touch 任務。
1>ClCompile:
1> fftw.cpp
1>c:\documents and settings\dell\桌面\fftw_p_s\fftw_p_s0\fftw.cpp(14): warning C4101: “input2”: 未參考的區域變數
1>c:\documents and settings\dell\桌面\fftw_p_s\fftw_p_s0\fftw.cpp(77): warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> d:\vs2010\vc\include\stdio.h(234) : 參見“fopen”的宣告
1>c:\documents and settings\dell\桌面\fftw_p_s\fftw_p_s0\fftw.cpp(82): warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> d:\vs2010\vc\include\stdio.h(234) : 參見“fopen”的宣告
1>c:\documents and settings\dell\桌面\fftw_p_s\fftw_p_s0\fftw.cpp(87): warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> d:\vs2010\vc\include\stdio.h(234) : 參見“fopen”的宣告
1>c:\documents and settings\dell\桌面\fftw_p_s\fftw_p_s0\fftw.cpp(94): warning C4996: 'fscanf': This function or variable may be unsafe. Consider using fscanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> d:\vs2010\vc\include\stdio.h(253) : 參見“fscanf”的宣告
1>c:\documents and settings\dell\桌面\fftw_p_s\fftw_p_s0\fftw.cpp(101): warning C4996: 'fscanf': This function or variable may be unsafe. Consider using fscanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> d:\vs2010\vc\include\stdio.h(253) : 參見“fscanf”的宣告
1>c:\documents and settings\dell\桌面\fftw_p_s\fftw_p_s0\fftw.cpp(119): error C2664: “Trans”: 不能將引數 2 從“long”轉換為“double []”
1> 從整型轉換為指標型別要求 reinterpret_cast、C 樣式轉換或函式樣式轉換
1>c:\documents and settings\dell\桌面\fftw_p_s\fftw_p_s0\fftw.cpp(140): error C2664: “Trans”: 不能將引數 2 從“long”轉換為“double []”
1> 從整型轉換為指標型別要求 reinterpret_cast、C 樣式轉換或函式樣式轉換
1>
1>生成失敗。
1>
1>已用時間 00:00:00.29
========== 全部重新生成: 成功 0 個,失敗 1 個,跳過 0 個 ==========
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/72731.html
標籤:基礎類
