简体   繁体   English

如何将正弦曲线图拟合到音频信号以确定 Python 中的频率和幅度?

[英]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.

相关问题 Python随时间查找音频和振幅 - Python find audio frequency and amplitude over time 如何确定“振荡”轨迹的幅度和频率 - How to determine the amplitude and the frequency of an “oscillating” trajectory 如何将静音的正弦曲线拟合到数据python? - how to fit a muted sinusoidal curve to data python? 如何获取在Python 3.x中记录的音频的频率和幅度? - How do I get the frequency and amplitude of audio that's being recorded in Python 3.x? 如何使用python绘制频谱或频率与整个音频文件的幅度的关系? - How to plot spectrum or frequency vs amplitude of entire audio file using python? 正弦曲线拟合问题:幅度和频率出来的太低 - Problems with sinusoidal curve fitting: amplitude and frequency come out too low 在Python中创建音频文件的幅度与频谱图 - Creating an amplitude vs frequency spectrogram of an audio file in Python 是否有可能将(频率,幅度)数据转换为Python中相应的(音频)声音? - Is it possible to convert (frequency, amplitude)-data to the corresponding (audio) sound in Python? 如何在python中修改幅度、频率和带宽值 - How to modify amplitude, frequency and bandwidth values in python 如何使用python打印调幅信号 - How to print a amplitude modulated signal using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM