Comments (3)
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "fftw3.h"
#include <iostream>
// 定义 WAV 文件头结构
typedef struct
{
char chunkId[4];
uint32_t chunkSize;
char format[4];
char subchunk1Id[4];
uint32_t subchunk1Size;
uint16_t audioFormat;
uint16_t numChannels;
uint32_t sampleRate;
uint32_t byteRate;
uint16_t blockAlign;
uint16_t bitsPerSample;
char subchunk2Id[4];
uint32_t subchunk2Size;
} WavHeader;
// 从 WAV 文件中读取数据
int32_t **readWavData(const char *filename, WavHeader *header)
{
FILE *file = fopen(filename, "rb");
if (!file)
{
printf("Failed to open file.\n");
return NULL;
}
// 读取 WAV 文件头
fread(header, sizeof(WavHeader), 1, file);
// 计算每个通道的样本数
int samplesPerChannel = header->subchunk2Size / (header->numChannels * sizeof(int32_t));
// 分配存储数据的内存
int32_t **data = (int32_t **)malloc(header->numChannels * sizeof(int32_t *));
for (int i = 0; i < header->numChannels; ++i)
{
data[i] = (int32_t *)malloc(samplesPerChannel * sizeof(int32_t));
}
// 读取音频数据
for (size_t i = 0; i < samplesPerChannel; ++i)
{
for (int j = 0; j < header->numChannels; ++j)
{
fread(&data[j][i], sizeof(int32_t), 1, file);
}
}
fclose(file);
return data;
}
int main()
{
const char *filename = "AKM_processed.wav";
WavHeader header;
int32_t **data = readWavData(filename, &header);
if (!data)
{
std::cout << "Failed to open wave file." << std::endl;
return 1;
}
// 打开输出文件
FILE *inputFile = fopen("fft_input.txt", "w");
FILE *outputFile = fopen("fft_output.txt", "w");
FILE *out_inputFile = fopen("ifft_output.txt", "w");
// 创建 FFTW 输入数组
int N = header.subchunk2Size / (header.numChannels * sizeof(int32_t)); // 数据长度
double *in = (double *)fftw_malloc(sizeof(double) * N);
fftw_complex *out = (fftw_complex *)fftw_malloc(sizeof(fftw_complex) * (N / 2 + 1));
fftw_plan dft = fftw_plan_dft_r2c_1d(N, in, out, FFTW_ESTIMATE);
fftw_plan idft = fftw_plan_dft_c2r_1d(N, out, in, FFTW_ESTIMATE); // 逆变换结果不对,16个对,16个不对,交叉出现对错
for (int i = 0; i < header.numChannels; i++)
{
for (int j = 0; j < N; j++)
{
in[j] = (double)data[i][j];
}
for (int j = 0; j < N; j++)
{
fprintf(inputFile, "%f", in[j]);
fprintf(inputFile, "\n");
}
fftw_execute(dft);
fftw_execute(idft);
for (int j = 0; j < N / 2 + 1; j++)
{
fprintf(outputFile, "%.6f + %.6fi ", out[j][0], out[j][1]);
fprintf(outputFile, "\n");
}
for (int j = 0; j < N; j++)
{
fprintf(out_inputFile, "%f", in[j] / N);
fprintf(out_inputFile, "\n");
}
}
// 关闭输出文件
fclose(inputFile);
fclose(outputFile);
fclose(out_inputFile);
// 释放内存和销毁计划
fftw_destroy_plan(dft);
fftw_destroy_plan(idft);
fftw_free(in);
fftw_free(out);
// 释放 WAV 数据的内存
for (int i = 0; i < header.numChannels; ++i)
{
free(data[i]);
}
free(data);
return 0;
}
from fftw3.
Related Issues (20)
- FFTW3LibraryDepends.cmake missing from autotools build HOT 4
- Error encountered when running 'make' after modifying Makefile.am
- Occasional failures in MPI part of the unit tests on ARM neoverse_v1 HOT 36
- Assessment of the difficulty in porting CPU architecture for fftw3 HOT 1
- FFTW SIMD Support HOT 1
- non-deterministic wisdom output HOT 4
- Link FFTW in Android Studio
- Fftw build failed when open ENABLE_OPENMP HOT 1
- Segmentation fault in check()
- Support DCT/DST-V-VIII HOT 1
- Smallbin double linked list corruption for specific data HOT 2
- FFTW threads: ld: error: undefined symbol: fftwf_threads_set_callback HOT 3
- `afft`?
- Compile application error
- Compilation Error HOT 3
- SVE Imlementation HOT 1
- Error: Unbound module "Pervasives" HOT 3
- "fftw_execute " case "Segmentation fault" HOT 4
- H
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fftw3.