【问题标题】:draw signal spectrum using matplotlib使用 matplotlib 绘制信号频谱
【发布时间】:2019-06-19 06:29:12
【问题描述】:

我有以下信号,我想做以下事情:

s= 4*np.cos(4*np.pi*pow(10,6)*t+30)+2*np.sin(8*np.pi*pow(10,6)*t+15)+np.cos(12*np.pi*pow(10,6)*t)+0.5*np.cos(16*np.pi*pow(10,6)*t) # the signal

我想使用 matplotlib 和 numpy 绘制信号频谱, 找到它的带宽并确定它是否是周期性的

我使用这里提供的代码(https://matplotlib.org/3.1.0/gallery/lines_bars_and_markers/spectrum_demo.html)

感谢您的帮助

【问题讨论】:

    标签: python-3.x numpy matplotlib


    【解决方案1】:

    我不确定我现在是否 100% 确定您想要做什么,但似乎绘制并返回您的函数的所有转折点应该对您的问题有很大帮助。

    因此你可以试试这个:

    import numpy as np
    import matplotlib.pyplot as plt
    from scipy.signal import argrelextrema
    
    def func(t):
    
        # messures the time in units of
        # pow(10,6)*t
    
        exp = 4*np.cos(4*np.pi*t+30)+\
        2*np.sin(8*np.pi*t+15)+\
        np.cos(12*np.pi*t)+\
        0.5*np.cos(16*np.pi*t) 
    
        return exp
    
    
    
    max_time = 2
    time_steps = 400
    
    # defining the signal
    X = np.linspace(0,max_time,time_steps)
    Y = func(X)
    
    # getting all the max and min values
    minimas = argrelextrema(Y, np.less)
    maximas = argrelextrema(Y, np.greater)
    
    # plot the singal
    plt.plot(X,Y)
    
    # plot minimas and maximas
    plt.scatter(X[minimas],Y[minimas],color='r')
    plt.scatter(X[maximas],Y[maximas],color='g')
    
    plt.xlabel('t*10**6')
    plt.ylabel('signal')
    plt.show()
    
    

    【讨论】:

    • 非常感谢,信号似乎是周期性的,但是当我绘制之前的代码时,它似乎不是,
    • 该函数对过度采样非常敏感...只需保留“max_time=10”并将“time_steps”从“100”更改为“200”。我无法进一步告诉你这似乎是表达的本质。你能告诉我这个函数应该代表什么,也许错误就在那里?
    • 应该代表一个信号在网络电缆中工作,我想绘制它的信号频谱。
    • 是的,这解释了非常高的频率。也许只是摆脱pow(10,6) 并以更直观的单位t_tilde = pow(10,6)*t 测量时间。那么在区间[0,10]中绘制函数比较合理。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-23
    • 2013-05-07
    • 1970-01-01
    • 2014-01-21
    • 2015-11-09
    相关资源
    最近更新 更多