[英]How to fit a sinusoidal graph to an audio signal in order to determine frequency and amplitude in Python?
I have an audio signal which has a form similar to the below.我有一个音频信号,其形式类似于以下内容。 I have tried to use Scipy's optimise library to fit a sine function to the data however this does not seem to work due to the form of the data.
我曾尝试使用 Scipy 的优化库将正弦 function 拟合到数据,但是由于数据的形式,这似乎不起作用。 How else could I fit a sine function to determine the frequency and amplitude?
我还能如何拟合正弦 function 来确定频率和幅度?
There are many ways to extract this information out of the data.有许多方法可以从数据中提取此信息。 You can (and probably should) apply some spectral analysis for the most accurate results.
您可以(并且可能应该)应用一些光谱分析以获得最准确的结果。 Check out SciPy's spectrogram , for instance.
例如,查看SciPy 的频谱图。 However, to quickly get an estimate of the frequency, you could just look at the zero-crossings:
但是,要快速估计频率,您可以只查看过零:
import numpy as np
import matplotlib.pyplot as plt
from math import pi
# Generate a 2 second array of data with millisecond resolution
time, timestep = np.linspace(0, 2, 2000, endpoint=False, retstep=True)
# Generate a constant frequency sine wave with varying amplitude
frequency = 42
amplitude = 1 / ((time - 1)**2 + 0.03)
data = amplitude * np.sin(2*pi*frequency*time)
plt.plot(time, data)
# Extract rising zero crossings
rising_zero_crossing_indices = np.where(np.diff(np.sign(data)) > 0)[0]
rising_zero_crossing_times = time[rising_zero_crossing_indices]
# Find the frequency
period = np.diff(rising_zero_crossing_times)
avg_frequency = 1/np.mean(period)
print(avg_frequency)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.