iphone低通滤波器

时间:2010-12-22 01:11:28

标签: iphone filter signal-processing openal

我正在尝试为iphone应用程序实现一个低通滤波器,在那里我录制一个声音,然后它会稍微低沉地播放;就像声音来自另一个房间。

我已经研究了录音和操作的不同选项,并且发现它有点令人困惑......数字信号处理根本不是一个强点。我主要研究OpenAL,在EFX库中有一个专门做我需要的过滤器,但是iPhone上没有包含EFX。有没有办法使用OpenAL为iPhone复制这种行为?是否有其他选项,如音频单元可以提供解决方案?

感谢您的帮助

编辑:

因此,在Tom的回答和链接之后,我想出了我认为正确的实现。但是,我根本没有得到消声效果,而只是减少音量。这是我目前的(汇总)代码:

使用AVAudioRecorder和以下设置录制文件:

[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];

[recordSetting setValue :[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
[recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
[recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];

然后我读入文件并使用以下代码对其进行转换:

// Read in the file using AudioFileOpenURL
AudioFileID fileID = [self openAudioFile:filePath];

// find out how big the actual audio data is
UInt32 fileSize = [self audioFileSize:fileID];

// allocate the memory to hold the file
SInt16 * outData = (SInt16 *)malloc(fileSize); 

// Read in the file to outData
OSStatus result = noErr;
result = AudioFileReadBytes(fileID, false, 0, &fileSize, outData);

// close off the file
AudioFileClose(fileID);

// Allocate memory to hold the transformed values
SInt16 * transformData = (SInt16 *)malloc(fileSize);

// Start the transform - Need to set alpha to 0.15 or below to have a noticeable affect
float alpha = 1;         

// Code as per Tom's example
transformData[0] = outData[0];

for(int sample = 1; sample < fileSize / sizeof(SInt16); sample ++) 
{
     transformData[sample] = transformData[sample - 1] + alpha * (outData[sample] - transformData[sample - 1]);
}

// Add the data to OpenAL buffer
NSUInteger bufferID;
// grab a buffer ID from openAL
alGenBuffers(1, &bufferID);

// Add the audio data into the new buffer
alBufferData(bufferID,AL_FORMAT_MONO16,transformData,fileSize,44100);

毕竟,然后我使用标准方法通过OpenAL播放它(我认为它对我的结果没有任何影响所以我不会在这里包含它。)

我已经在变换之前和之后跟踪了结果,并且它们对我来说似乎是正确的,即之前的值正如我预期的那样正向和负向变化,并且for循环肯定会使这些值变平。但正如我之前提到的那样,我只看到(在我看来)减少音量,所以我能够增加收益并取消我刚刚完成的工作。

似乎我必须正在研究错误的价值观。我在这里做错了什么建议?

2 个答案:

答案 0 :(得分:8)

Tom的答案是以下递归过滤器:

y[n] = (1 - a)*y[n-1] + a*x[n]

H(z) = Y(z)/X(z) = a / (1 - (1 - a)*1/z)

我将在Python / pylab中绘制a = 0.25,a = 0.50和a = 0.75:

from pylab import *

def H(a, z):
    return a / (1 - (1 - a) / z)

w = r_[0:1000]*pi/1000
z = exp(1j*w)
H1 = H(0.25, z)
H2 = H(0.50, z)
H3 = H(0.75, z)
plot(w, abs(H1), 'r') # red
plot(w, abs(H2), 'g') # green
plot(w, abs(H3), 'b') # blue

alt text

Pi弧度/采样是奈奎斯特频率,是采样频率的一半。

如果这个简单的过滤器不合适,请尝试二阶巴特沃斯滤波器:

# 2nd order filter:
# y[n] = -a[1]*y[n-1] - a[2]*y[n-2] + b[0]*x[n] + b[1]*x[n-1] + b[2]*x[n-2]

import scipy.signal as signal

# 2nd order Butterworth filter coefficients b,a
# 3dB cutoff = 2000 Hz
fc = 2000.0/44100
b, a = signal.butter(2, 2*fc)

# b = [ 0.01681915,  0.0336383 ,  0.01681915]
# a = [ 1.        , -1.60109239,  0.66836899]

# approximately:
# y[n] = 1.60109*y[n-1] - 0.66837*y[n-2] + 
#        0.01682*x[n] + 0.03364*x[n-1] + 0.01682*x[n-2]

# transfer function
def H(b,a,z):
    num = b[0] + b[1]/z + b[2]/(z**2)
    den = a[0] + a[1]/z + a[2]/(z**2)
    return num/den

H4 = H(b, a, z)
plot(w, abs(H4))
# show the corner frequency
plot(2*pi*fc, sqrt(2)/2, 'ro')  
xlabel('radians')

alt text

以3dB截止频率fc=2000评估测试信号:

fc = 2000.0/44100
b, a = signal.butter(2, 2*fc)

# test signal at corner frequency (signed 16-bit)
N = int(5/fc)  # sample for 5 cycles
x = int16(32767 * cos(2*pi*fc*r_[0:N]))

# signed 16-bit output
yout = zeros(size(x), dtype=int16)

# temp floats
y  = 0.0    
y1 = 0.0
y2 = 0.0

# filter the input
for n in r_[0:N]:
    y = (-a[1] * y1 + 
         -a[2] * y2 + 
          b[0] * x[n]   + 
          b[1] * x[n-1] + 
          b[2] * x[n-2])
    # convert to int16 and saturate
    if y > 32767.0:    yout[n] = 32767
    elif y < -32768.0: yout[n] = -32768
    else:              yout[n] = int16(y)
    # shift the variables
    y2 = y1
    y1 = y

# plots
plot(x,'r')       # input in red
plot(yout,'g')    # output in green
# show that this is the 3dB point
plot(sqrt(2)/2 * 32768 * ones(N),'b-')
xlabel('samples')

alt text

答案 1 :(得分:0)

我对数字信号处理知之甚少,但我相信您可以使用linear interpolation.来近似低通滤波器。有关信息,请参阅this Wikipedia article的末尾。

这是一个可能会给您一个想法的代码段。 alpha 是滤镜系数。降低alpha值会增加消声效果,IIRC。


output_samples[0] = input_samples[0];

for(int sample = 1; sample < num_samples; sample ++) {
  output_samples[sample] = output_samples[sample - 1] + 
    alpha * (input_samples[sample] - output_samples[sample - 1]);
}

编辑:我认为这里的alpha通常介于0和1之间。