gpt4 book ai didi

iPhone低通滤波器

转载 作者:行者123 更新时间:2023-12-03 18:26:09 25 4
gpt4 key购买 nike

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

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

感谢您的帮助

编辑:

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

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

看来我的值(value)观一定是错误的。对我在这里做错了什么有什么建议吗?

最佳答案

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

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

关于iPhone低通滤波器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4505405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com