diff --git a/README.md b/README.md index d3f0252..2980a84 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ - Git - Windows函数库相关函数 - IEEE POSIX标准下的相关函数 +- FFTW 使用快速傅里叶变换分析音频文件 ## 主要功能 ### 按照WAV格式标准读取WAV格式文件 diff --git a/Unused/FFT/FFT.cpp b/Unused/FFT/FFT.cpp new file mode 100644 index 0000000..7b75a08 --- /dev/null +++ b/Unused/FFT/FFT.cpp @@ -0,0 +1,88 @@ +#include "pch.h" +#pragma comment(lib, "E:\\Lib\\libfftw3-3.lib") +#pragma comment(lib, "E:\\Lib\\libfftw3f-3.lib") +#pragma comment(lib, "E:\\Lib\\libfftw3l-3.lib") +#include +#include +#include "fftw3.h" +#include +#include +#include +//ƵΪ FR , Ϊ1024 (NN),ôNƵ X(N) = (FR / NN) * N +#define FR 44100 +class Get_RGB { +public: + static void fftw3(float *in,int NN, int &hz); + double hztospectrun(double hz); + static void spectral_color(double &r, double &g, double &b, double l); + static void GetRGB( float* in,int NN, double &R, double &G,double &B){//RGBֵ + int hz; + double l=0; + fftw3(in,NN, hz); + spectral_color(R, G, B, l); + } +}; +void Get_RGB::fftw3(float *in ,int NN, int &frequency) +{ + struct Max { + int id=0; + int value=0; + }M; + + int i; + fftwf_complex *out = NULL; // fftwf_complex --> Ϊfloat汾 + fftwf_plan p; + in = (float *)fftwf_malloc(sizeof(float) * NN); + out = (fftwf_complex *)fftwf_malloc(sizeof(fftwf_complex) *NN); + // Ҷ任 + p = fftwf_plan_dft_r2c_1d(NN, in, out, FFTW_ESTIMATE); + fftwf_execute(p); + // + for (i = 0; i < NN; i++) + { + float len = sqrt(out[i][0] * out[i][0] + out[i][1] * out[i][1]); + if (len > M.value) { + M.id=i ; + } + // printf("%d ",i); + // printf("%.2f ", len); + } + frequency = M.id*(FR / NN); + //frequency = M.id*(FR / NN); + // ͷԴ + + + return; +} +int main() { + float in[10000]; + double R, G, B; + int NN; + std::cin >> NN; + for (int i=0;i < NN;i++) { + std::cin >> in[i]; + } + Get_RGB::GetRGB(in,NN, R, G, B); + std::cout << R << " " << G << " " << B; + std::cout << "\n"<< R << " " << G << " " << B; +} +double Get_RGB::hztospectrun(double hz) { + double hbox; + hbox = hz; + for (;hbox > 800;hbox = hbox / 2); + return hbox; +} +void Get_RGB::spectral_color(double &r, double &g, double &b, double l) // RGB <0,1> <- lambda l <400,700> [nm] +{ + double t; r = 0.0; g = 0.0; b = 0.0; + if ((l >= 400.0) && (l < 410.0)) { t = (l - 400.0) / (410.0 - 400.0); r = +(0.33*t) - (0.20*t*t); } + else if ((l >= 410.0) && (l < 475.0)) { t = (l - 410.0) / (475.0 - 410.0); r = 0.14 - (0.13*t*t); } + else if ((l >= 545.0) && (l < 595.0)) { t = (l - 545.0) / (595.0 - 545.0); r = +(1.98*t) - (t*t); } + else if ((l >= 595.0) && (l < 650.0)) { t = (l - 595.0) / (650.0 - 595.0); r = 0.98 + (0.06*t) - (0.40*t*t); } + else if ((l >= 650.0) && (l < 700.0)) { t = (l - 650.0) / (700.0 - 650.0); r = 0.65 - (0.84*t) + (0.20*t*t); } + if ((l >= 415.0) && (l < 475.0)) { t = (l - 415.0) / (475.0 - 415.0); g = +(0.80*t*t); } + else if ((l >= 475.0) && (l < 590.0)) { t = (l - 475.0) / (590.0 - 475.0); g = 0.8 + (0.76*t) - (0.80*t*t); } + else if ((l >= 585.0) && (l < 639.0)) { t = (l - 585.0) / (639.0 - 585.0); g = 0.84 - (0.84*t); } + if ((l >= 400.0) && (l < 475.0)) { t = (l - 400.0) / (475.0 - 400.0); b = +(2.20*t) - (1.50*t*t); } + else if ((l >= 475.0) && (l < 560.0)) { t = (l - 475.0) / (560.0 - 475.0); b = 0.7 - (t)+(0.30*t*t); } +} \ No newline at end of file diff --git a/windows/main.cpp b/windows/main.cpp index 9de8492..ad0925b 100644 --- a/windows/main.cpp +++ b/windows/main.cpp @@ -22,7 +22,7 @@ int main(int argc, char * argv[]) { glutInitWindowPosition(100, 100); glutInitWindowSize(700, 700); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); - int hw = init_test_wav(60); + int hw = init_test_wav(120); create_fields(hw); nwd.create("Fields"); nwd.show();