【问题标题】:iphone low pass filteriphone低通滤波器
【发布时间】:2011-05-29 04:06:13
【问题描述】:

我正在尝试为 iphone 应用程序实现低通滤波器,在该应用程序中我录制声音,然后播放时声音会略微低沉;好像声音是从另一个房间传来的。

我研究了音频录制和处理的不同选项,发现它有点令人困惑……数字信号处理根本不是一个强项。我主要研究了 OpenAL,在 EFX 库中,有一个过滤器专门满足我的需求,但 iPhone 中不包含 EFX。有没有办法使用 iPhone 的 OpenAL 来复制这种行为?是否有其他选项(例如 Audio Units)可以提供解决方案?

感谢您的帮助

编辑:

所以在汤姆的回答和链接之后,我想出了我认为正确的实现。但是,我根本没有得到消音效果,而只是音量减少。这是我目前拥有的(摘要)代码:

使用 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 循环肯定会使这些值变平。但正如我之前提到的,我只看到(在我看来)音量减少,所以我能够增加增益并抵消我刚刚所做的。

看来我一定是在处理错误的价值观。关于我在这里做错了什么的任何建议?

【问题讨论】:

  • 你好....你有什么发现吗?

标签: iphone filter signal-processing openal


【解决方案1】:

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

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')

在 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')

【讨论】:

  • 从我最近在 dsp 上所做的所有阅读中,这对我来说很有意义。我会尽快尝试(即明天早上)。我要澄清的一点是,从录制文件中读取的数据以整数数组的形式返回,并且从我所做的简单语音测试中,它们的范围从 -7000 到 7000。我猜这是每个样本的直线幅度,并且是输入到我上面的 outData 变量中的内容。输入该算法的数据是否正确?
  • 这是一个绝妙的答案——也是一个很好的 pylab 入门教程!
【解决方案2】:

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

这里有一个代码 sn-p 可能会给你一个想法。 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 之间。

【讨论】:

  • 感谢您的回复。该功能很棒,我肯定会使用它,但我也想知道如何首先获取低级数据。我猜我将不得不为此使用Core Audio?抱歉,我无法调查自己,但我只是在帮助 iPhone 开发人员使用此功能并且没有测试环境。
  • 我猜你将不得不使用 Core Audio。我刚刚发现了另一个与此相关的问题,显然 OP 在 iOS 上使用 Core Audio 作为他的低通滤波器。这是链接:stackoverflow.com/questions/4162504/…
  • 和您的示例一样,我尝试实现与您的评论相关联的巴特沃斯过滤器,但看到的结果与您的函数相同(即体积减少)。让我相信我没有正确处理采样数据
  • 除了音量减少之外,过滤器似乎还有效吗?也就是说,对于低通滤波器,您是否听到较高频率的降低?一般来说,这些简单的滤波器绝对可以降低整体音量,因为它们只允许部分输入信号通过并阻止其余部分。您可以在过滤后提高音量以进行补偿。
猜你喜欢
  • 2011-08-31
  • 1970-01-01
  • 2014-07-29
  • 2019-04-29
  • 2015-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多