基于我在GitHub上的previous question和pitch detector。我设法检测出我的样本的主导频率。但是像扎普所说的那样,切断整个样本是不礼貌的。我的问题是,如果我已经将样本从时间域转换到频域,那么我该如何返回并将频率转换为时域?
我的接近时间 - >频率
ConvertInt16ToFloat(THIS, dataBuffer, outputBufferFrequency, bufferCapacity);
maxFrames = 32;
log2n = log2f(maxFrames);
n = 1 << log2n;
assert(n == maxFrames);
nOver2 = maxFrames/2;
bufferCapacity = maxFrames;
COMPLEX_SPLIT A;
A.realp = (float *)malloc(nOver2 * sizeof(float));
A.imagp = (float *)malloc(nOver2 * sizeof(float));
fftSetup = vDSP_create_fftsetup(log2n, FFT_RADIX2);
vDSP_ctoz((COMPLEX*)outputBuffer, 2, &A, 1, nOver2);
// Carry out a Forward FFT transform.
vDSP_fft_zrip(fftSetup, &A, stride, log2n, FFT_FORWARD);
// The output signal is now in a split real form. Use the vDSP_ztoc to get
// a split real vector.
vDSP_ztoc(&A, 1, (COMPLEX *)outputBuffer, 2, nOver2);
// Determine the dominant frequency by taking the magnitude squared and
// saving the bin which it resides in.
float dominantFrequency = 0;
int bin = -1;
for (int i=0; i<n; i+=2) {
float curFreq = MagnitudeSquared(outputBuffer[i], outputBuffer[i+1]);
if (curFreq > dominantFrequency) {
dominantFrequency = curFreq;
bin = (i+1)/2;
}
}
//First faild approach to convert frequency->ti
vDSP_ctoz((COMPLEX*)outputBufferFrequency, 2, &A, 1, nOver2);
vDSP_fft_zrip(fftSetup, &A, stride, log2n, FFT_INVERSE);
vDSP_ztoc(&A, 1, (COMPLEX *)outputBufferFrequency, 2, nOver2);
ConvertFloatToInt16(THIS, outputBufferFrequency, outputBufferTime, bufferCapacity);
// checking is preTimeToFrequency buffer is same as postTimeToFrequency buffer
// and is not, atm.
for (int i=0; i<bufferCapacity; i++) {
printf("%i != %i",((SInt16*)dataBuffer)[i], ((SInt16*)outputBufferTime)[i]);
if (((SInt16*)dataBuffer)[i] != ((SInt16*)outputBufferTime)[i]){
printf("dupa\n");
}
}
void ConvertInt16ToFloat(RIOInterface* THIS, void *buf, float *outputBuf, size_t capacity) {
AudioConverterRef converter;
OSStatus err;
size_t bytesPerSample = sizeof(float);
AudioStreamBasicDescription outFormat = {0};
outFormat.mFormatID = kAudioFormatLinearPCM;
outFormat.mFormatFlags = kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked;
outFormat.mBitsPerChannel = 8 * bytesPerSample;
outFormat.mFramesPerPacket = 1;
outFormat.mChannelsPerFrame = 1;
outFormat.mBytesPerPacket = bytesPerSample * outFormat.mFramesPerPacket;
outFormat.mBytesPerFrame = bytesPerSample * outFormat.mChannelsPerFrame;
outFormat.mSampleRate = THIS->sampleRate;
const AudioStreamBasicDescription inFormat = THIS->streamFormat;
UInt32 inSize = capacity*sizeof(SInt16);
UInt32 outSize = capacity*sizeof(float);
err = AudioConverterNew(&inFormat, &outFormat, &converter);
err = AudioConverterConvertBuffer(converter, inSize, buf, &outSize, outputBuf);
}
void ConvertFloatToInt16(RIOInterface* dev, float *buf, void *outputBuf, size_t capacity) {
AudioConverterRef converter;
OSStatus err;
size_t bytesPerSample = sizeof(short);
AudioStreamBasicDescription outFormat = {0};
outFormat.mFormatID = kAudioFormatLinearPCM;
outFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
outFormat.mBitsPerChannel = 8 * bytesPerSample;
outFormat.mFramesPerPacket = 1;
outFormat.mChannelsPerFrame = 1;
outFormat.mBytesPerPacket = bytesPerSample * outFormat.mFramesPerPacket;
outFormat.mBytesPerFrame = bytesPerSample * outFormat.mChannelsPerFrame;
outFormat.mSampleRate = dev->sampleRate;
const AudioStreamBasicDescription inFormat = dev->streamFormat;
UInt32 inSize = capacity*sizeof(float);
UInt32 outSize = capacity*sizeof(SInt16);
err = AudioConverterNew(&inFormat, &outFormat, &converter);
err = AudioConverterConvertBuffer(converter, inSize, buf, &outSize, outputBuf);
}
阅读this示例代码后的第二种方法
vDSP_fft_zrip(fftSetup, &A, stride, log2n, FFT_INVERSE);
vDSP_ztoc(&A, 1, (COMPLEX *)outputBufferFrequency, 2, nOver2);
float scale = 0.5/maxFrames;
vDSP_vsmul(outputBufferFrequency, 1, &scale, outputBufferFrequency, 1, maxFrames);
ConvertFloatToInt16(THIS, outputBufferFrequency, outputBufferTime, bufferCapacity);
最后!在我阅读this anwser之后,请阅读一些我设法解决问题的示例代码。
我的工作代码。
变量streamFormat在eles的某个地方初始化,我把初始化ConvertFloatToInt16只是为了更好地查看我使用的内容。
ConvertInt16ToFloat(THIS, dataBuffer, outputBufferFrequency, bufferCapacity);
//TIME -> FREQUENCU
vDSP_ctoz((COMPLEX*)outputBufferFrequency, 2, &A, 1, nOver2);
// Carry out a Forward FFT transform.
vDSP_fft_zrip(fftSetup, &A, stride, log2n, FFT_FORWARD);
// The output signal is now in a split real form. Use the vDSP_ztoc to get
// a split real vector.
vDSP_ztoc(&A, 1, (COMPLEX *)outputBufferFrequency, 2, nOver2);
//FREQUENCY -> TIME
//Back to time domain
vDSP_ctoz((COMPLEX*)outputBufferFrequency, 2, &A, 1, nOver2);
vDSP_fft_zrip(fftSetup, &A, stride, log2n, FFT_INVERSE);
float scale = (float) 1.0 / (2 * maxFrames);;
vDSP_vsmul(A.realp, 1, &scale, A.realp, 1, nOver2);
vDSP_vsmul(A.imagp, 1, &scale, A.imagp, 1, nOver2);
vDSP_ztoc(&A, 1, (COMPLEX *)outputBufferFrequency, 2, nOver2);
ConvertFloatToInt16(THIS, outputBufferFrequency, outputBufferTime, bufferCapacity);
void ConvertInt16ToFloat(RIOInterface* THIS, void *buf, float *outputBuf, size_t capacity) {
AudioConverterRef converter;
OSStatus err;
size_t bytesPerSample = sizeof(float);
AudioStreamBasicDescription outFormat = {0};
outFormat.mFormatID = kAudioFormatLinearPCM;
outFormat.mFormatFlags = kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked;
outFormat.mBitsPerChannel = 8 * bytesPerSample;
outFormat.mFramesPerPacket = 1;
outFormat.mChannelsPerFrame = 1;
outFormat.mBytesPerPacket = bytesPerSample * outFormat.mFramesPerPacket;
outFormat.mBytesPerFrame = bytesPerSample * outFormat.mChannelsPerFrame;
outFormat.mSampleRate = THIS->sampleRate;
THIS->streamFloatFormat = outFormat;
const AudioStreamBasicDescription inFormat = THIS->streamFormat;
UInt32 inSize = capacity*sizeof(SInt16);
UInt32 outSize = capacity*sizeof(float);
err = AudioConverterNew(&inFormat, &outFormat, &converter);
err = AudioConverterConvertBuffer(converter, inSize, buf, &outSize, outputBuf);
}
void ConvertFloatToInt16(RIOInterface* THIS, float *buf, void *outputBuf, size_t capacity) {
AudioConverterRef converter;
OSStatus err;
AudioStreamBasicDescription asbd = {0};
size_t bytesPerSample;
bytesPerSample = sizeof(SInt16);
asbd.mFormatID = kAudioFormatLinearPCM;
asbd.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
asbd.mBitsPerChannel = 8 * bytesPerSample;
asbd.mFramesPerPacket = 1;
asbd.mChannelsPerFrame = 1;
asbd.mBytesPerPacket = bytesPerSample * asbd.mFramesPerPacket;
asbd.mBytesPerFrame = bytesPerSample * asbd.mChannelsPerFrame;
asbd.mSampleRate = sampleRate;
THIS->streamFormat = asbd;
const AudioStreamBasicDescription outFormat = THIS->streamFormat;
const AudioStreamBasicDescription inFormat = THIS->streamFloatFormat;
UInt32 inSize = capacity*sizeof(float);
UInt32 outSize = capacity*sizeof(SInt16);
err = AudioConverterNew(&inFormat, &outFormat, &converter);
err = AudioConverterConvertBuffer(converter, inSize, buf, &outSize, outputBuf);
}
答案 0 :(得分:1)
逆傅里叶变换将您的离散信号从频域转换回时域。
你可以像这样执行逆傅立叶变换 -
vDSP_fft_zrip(fftSetup, &A, stride, log2n, FFT_INVERSE);
然后必须相应地缩放。缩放因子见于 图2-6 Apple在vDSP Programming Guide中找到的缩放因子摘要
这里有一个很好的例子Forward and Inverse FFT using Accelerate。
在阅读更新后,如果您尝试在频域中移位信号然后将其转换回时域,则会涉及到相当多的内容。我建议您从DSP Dimension上的Pitch shifting using the Fourier transform阅读这篇文章。