0

我正在寻找一个数字信号,并使用 python 将其与 0 和 1 调制成模拟信号。我遇到的问题是波浪在移动时似乎朝着错误的方向发展。到目前为止,这是我的代码,我知道它并不漂亮。

import numpy as np
import math

import matplotlib.pyplot as plot

def plotPoint():
    current = 0
    amplitude = 1
    frequency = 2000
    X = []
    Y1 = []
    Y2 = []
    signal= [1,0,1,1,0,1,0,1,0,0]


    while current < 10000:
        if current < 1000 and signal[0] == 1:
            Y2.append(1)
        elif current < 2000 and current >= 1000 and signal[1] == 1:
            Y2.append(1)
        elif current < 3000 and current >= 2000 and signal[2] == 1:
            Y2.append(1)
        elif current < 4000 and current >= 3000 and signal[3] == 1:
            Y2.append(1)
        elif current < 5000 and current >= 4000 and signal[4] == 1:
            Y2.append(1)
        elif current < 6000 and current >= 5000 and signal[5] == 1:
            Y2.append(1)
        elif current < 7000 and current >= 6000 and signal[6] == 1:
            Y2.append(1)
        elif current < 8000 and current >= 7000 and signal[7] == 1:
            Y2.append(1)
        elif current < 9000 and current >= 8000 and signal[8] == 1:
            Y2.append(1)
        elif current < 10000 and current >= 9000 and signal[9] == 1:
            Y2.append(1)
        else:
            Y2.append(0)
    
    
        
        if Y2[-1] == 1:
            point2 = amplitude*math.sin((2*np.pi/frequency)*current)
        else:
            point2 = amplitude*math.sin((2*np.pi/(frequency/2)*current)
    
        X.append(current)
        Y1.append(point2)
   
    
        current += 1

    plot.plot(X,Y1)
    plot.plot(X,Y2)


    plot.title('Digital Signal')
    plot.xlabel('Time')
    plot.ylabel('Amplitude')
    plot.grid(True, which='both')
    #plot.axhline(y=0, color='k')

    plot.show()


plotPoint()

你可以看到数字和模拟信号都显示出来了。我使用了 2kHz 的频率作为载波,您可以在这张图片中看到它何时发生偏移。在此处输入图像描述 做错了什么?我该如何补救?

4

2 回答 2

1

此代码适用于频率和间隔的任意组合:

amplitude = 1
frequencies = [1200, 4000]
X = []
Y1 = []
Y2 = []
signal= [1,0,1,1,0,1,0,1,0,0]
interval = 1000
phase = 0

for s in signal:
    frequency = frequencies[s]
    for t in range (interval):
        phase += frequency/1000000 * math.pi * 2
        Y2.append(s)
        Y1.append(math.sin(phase) * amplitude)

X = range(len(signal)*interval)
ax, fig = plot.subplots(figsize=(16, 2))
plot.plot(X,Y1)
plot.plot(X,Y2)

plot.title('Digital Signal')
plot.xlabel('Time')
plot.ylabel('Amplitude')
plot.grid(True, which='both')

在此处输入图像描述

于 2020-09-21T21:00:33.690 回答
1

根据巴尼的建议,这将需要对您的代码进行小幅更正:

if Y2[-1] == 1:
    point2 = amplitude*math.sin((2*np.pi/(frequency/2))*current)
else:
    point2 = amplitude*math.sin((2*np.pi/(frequency/4)*current))

在此处输入图像描述

这是你想要的?

于 2020-09-21T20:43:22.677 回答