听起来是个有趣的项目。
您可以通过跨样本“模糊”来实现低通滤波器,并通过其他简单的数学来实现高通(现在不记得那是什么了)
但是,如果您正在处理音频,您最终会希望将信号转换为频域并返回。最好的开源库是 FFTW3,并且 gem 中有一个 Ruby 绑定fftw3
-narray
如果您尚未使用它,则无论如何都应该考虑使用它,因为它在处理由 1000 个单个样本组成的数组时表现非常好。
要开始转换到频域:
require 'narray'
require 'fftw3'
# You'll need to feed in real-world data in audio_segment
# This generates white noise -1.0 to 1.0
audio_segment = 2.0 * ( NArray.float(1024).random() - 0.5 )
# To avoid edges of the window looking like high-frequency changes,
# you need to apply a window function. This is just a multiplier for each sampel point
# Look up Hann window on Wikipedia, the maths is very simple.
# hann_window is a simple 1024 NArray of floats, and you can re-use the same one each time
audio_window = audio_segment * hann_window
# This does FFT magic
frequency_domain_window = FFTW3.fft(audio_window, -1)
# What you do next depends on the processing you need to do. Typically you'll want to
# re-normalise the data (as FFTW doesn't do that for you)
frequency_domain_window *= 1.0/1024
# This is a very crude "notch filter" that reduces amplitude of some mid frequencies
frequency_domain_window[100..200] *= 0.3
# Convert back to samples in time (but we still are in a Hann window)
processed_audio_window = (FFTW3.ifft( frequency_domain_window, 0 )).real
# Next you need to do an inverse of the Hann window
# After then you'll want to step forward say 256 samples, and repeat the process
# whilst averaging windows together where they overlap . . .
抱歉,这不是一段功能齐全的代码,但希望能给你足够的指导去玩!