简体   繁体   English

在 Python 中计算频率调制

[英]Calculate Frequency Modulation in Python

I'm trying to calculate Frequency Modulation (radio context) for a given audio file (12 seconds), I managed to do it on a sine wave with the following formula:我正在尝试为给定的音频文件(12 秒)计算频率调制(无线电上下文),我设法使用以下公式在正弦波上进行计算:

fm = np.sin(TWO_PI * (fc + b * data) * t)

Where fc is the Carrier Frequency, b is the Modulation index, data is the audio file and t is the time vector.其中 fc 是载波频率,b 是调制指数,数据是音频文件,t 是时间向量。

But I can't seem to get it to work on the audio file,但我似乎无法让它处理音频文件,

this is what I have so far:这是我到目前为止:

在此处输入图片说明

As you can see, you can't really understand when the frequency changes, I can zoom in but that's not really reliable and confusing I would be happy to hear other ways.如您所见,您无法真正理解频率何时发生变化,我可以放大,但这并不可靠且令人困惑,我很乐意听到其他方式。

Here is my full code:这是我的完整代码:

import scipy.io.wavfile
import matplotlib.pyplot as plt
import numpy as np

def generateSignalFM(t,data):
    TWO_PI = 2 * np.pi
    fc = 10000
    b = 5

    fm = np.sin(TWO_PI * (fc + b * np.array(data)) * t) # calculaying frequency modulation

    fig, axs = plt.subplots(nrows=2, ncols=1)
    fig.tight_layout()

    axs[1].plot(t,fm)
    axs[1].set_xlabel("Time(s)")
    axs[1].set_ylabel("Amplitude")
    axs[1].set_title("Modulated Signal (FM)")

    axs[0].plot(t,data)
    axs[0].set_xlabel("Time(s)")
    axs[0].set_ylabel("Amplitude")
    axs[0].set_title("Original Signal")
    plt.show()

samplerate, data = scipy.io.wavfile.read("musicSample.wav")
sample_for = 12
start_time = 30*samplerate # start from 30 seconds
end_time = start_time + (samplerate * sample_for) # sample for 1 second
split_data= data[start_time:end_time]
time = np.arange(0,sample_for,1/samplerate) #sample 1 second

You can see your signal's PSD after modulation by using "welch" algorithm.您可以使用“welch”算法在调制后查看信号的 PSD。 You can do it by scipy.signal.welch and something like this will be shown:你可以通过scipy.signal.welch来做到这scipy.signal.welch ,类似的东西会显示出来: 在此处输入图片说明

This is the example for fc = 100 and sampleRate=1000这是 fc = 100 和 sampleRate=1000 的示例

To see this, you should use the following context in your code:要看到这一点,您应该在代码中使用以下上下文:

import scipy.signal as ss
FM = ss.welch(fm, fs=sampleRate, nperseg=2048, noverlap=1024, nfft=2048)
axs[0].plot(FM[0],FM[1])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM