2

我有一个函数,我应该绘制 10MHz 和 11.4MHz 之间的幅度谱。这是函数: cos(6.72*(10**7*t) + 3.2*sin(5*(10**5*t))) 我知道情节应该是什么样子,但我的完全错误. 我很确定这是一个编程错误,而不是数学/理论错误,因为我所做的正是 MatLab 示例所显示的,但我觉得 Python 正在做一些我不理解的事情。我应该以 10ns 的间隔对该函数进行 8192 次采样,将其乘以汉明窗,然后绘制 10MHz 和 11.4MHz 之间的幅度谱(以 dB 为单位)。载波 (10.7MHz) 应该在 55 dB 左右。第二对边带是 60 dB 处的最大幅度。然后,第五对边带是 20 dB 点,大约是 40 dB。

import numpy
import scipy
import scipy.fftpack
import matplotlib
import matplotlib.pyplot as plt
from scipy import pi
import pylab
from pylab import *
import cmath
import sys
sys.setrecursionlimit(10000)

samples = 8192

#Defining the test function
t = scipy.linspace(0.01, 32/390625, samples, False)   #Sample samples times at 10ns apart
#The samples is non-inclusive, it goes from 0 to samples-1.
x_t = cos(6.72*(10**7*t) + 3.2*sin(5*(10**5*t)))  #Define x(t)
acc = lambda t: (x_t)                             #Define x[t] in terms of t as a variable in Python's eyes 

signal = acc(t)                 #Make signal a function of t
plt.subplot(211)                #Set up a plot
plt.xlabel('Ohmega (Hz)')       #Label x axis
plt.ylabel('Amplitude of sampled x(t) (dB)')  #Label y axis

window = numpy.hamming(samples)    #Make a hamming window 


w = scipy.linspace(0, 100*10**6, samples, False)   #Create Ohmega between 1/(10ns)
signal = signal * window        #Multiply by the hamming window
signal = scipy.fft(signal)      #Take the FFT
signal = abs(20*log10(signal))  #Get the magnitude in dB scale

plt.xlabel('Ohmega')            #Label x axis
plt.ylabel('|F[w]|')            #Label y axis
#marker, stemlines, baseline = stem(w,signal, 'b-..')    #Plot with stemlines
plot(w,signal, 'b-')
plt.xlim(10*10**6, 11.4*10**6)  #Set x-limits

plt.show()                      #Show the plot

谢谢你的帮助!

编辑:

我现在的代码:

import numpy
import scipy
import scipy.fftpack
import matplotlib
import matplotlib.pyplot as plt
from scipy import pi
import pylab
from pylab import *
import cmath
import sys
sys.setrecursionlimit(10000)

samples = 8192

#Defining the test function
t = scipy.linspace(0.01, 32/390625, samples, False)         #Sample samples times at 10ns apart
#The samples is non-inclusive, it goes from 0 to samples-1.
x_t = cos(6.72*(10**7*t) + 3.2*sin(5*(10**5*t)))            #Define x(t)
acc = lambda t: cos(6.72*(10**7*t) + 3.2*sin(5*(10**5*t)))  #Define x[t] in terms of t as a variable in Python's eyes 

#signal = acc(t)                 #Make signal a function of t
plt.subplot(211)                #Set up a plot
plt.xlabel('Ohmega (Hz)')       #Label x axis
plt.ylabel('Amplitude of sampled x(t) (dB)')  #Label y axis

window = numpy.hamming(samples)    #Make a hamming window 


w = scipy.linspace(0.01, 100*10**6, samples, False)   #Create Ohmega between 1/(10ns)
signal = lambda t: abs(20*log10(scipy.fft(acc*window)))
#acc = acc * window        #Multiply by the hamming window
#acc = scipy.fft(acc)      #Take the FFT
#acc = abs(20*log10(acc))  #Get the magnitude in dB scale

plt.xlabel('Ohmega')            #Label x axis
plt.ylabel('|F[w]|')            #Label y axis
#marker, stemlines, baseline = stem(w,signal, 'b-..')    #Plot with stemlines

plot(w,signal, 'b-')
plt.xlim(10*10**6, 11.4*10**6)  #Set x-limits

plt.show()                      #Show the plot

错误...:

    Traceback (most recent call last):
  File "/home/hollis/Documents/ELEN 322/ELEN_322_#2.py", line 39, in <module>
    plot(w,signal, 'b-')
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2467, in plot
    ret = ax.plot(*args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 3893, in plot
    for line in self._get_lines(*args, **kwargs):
  File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 322, in _grab_next_args
    for seg in self._plot_args(remaining, kwargs):
  File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 300, in _plot_args
    x, y = self._xy_from_xy(x, y)
  File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 240, in _xy_from_xy
    raise ValueError("x and y must have same first dimension")
ValueError: x and y must have same first dimension

虽然我之前已经看到并修复了这个错误,但我现在不知道如何修复它。显然,一个信号被采样了 8192 次,而决定采样位置的变量是不同的维度。我对这个很迷茫...

4

2 回答 2

1

您的错误可能来自您定义的方式x(t)

x_t = cos(6.72*(10**7*t) + 3.2*sin(5*(10**5*t)))  #Define x(t)
acc = lambda t: (x_t)  #Define x[t] in terms of t as a variable in Python's eyes

scipy.linspace(0.01, 32/390625, samples, False)当 Python 执行第一行时,它使用 t ( )的当前值将给定公式的值分配给变量 x_t 。它不解释t为变量。

要解决此问题,请通过以下方式更改第二行:

acc = lambda t: cos(6.72*(10**7*t) + 3.2*sin(5*(10**5*t)))

编辑:刚刚注意到你对signal. 写:(signal = acc(t)即使你修复acc了)只会将函数的结果acc(t)(当前值为 t)分配给变量信号。它不会是一个函数。(你为什么不直接使用 acc 而不是 signal,因为 acc 是 t 的函数?)

于 2013-05-04T15:26:23.317 回答
0

不确定您是否仍在为此工作。我确实找到了这篇文章:Matlab 中的 FFT 和 numpy / scipy 给出了不同的结果,这可能会有所启发。

该帖子建议在 Scipy 中使用转置,因为 Matlab 将 fft 应用于矩阵的列,numpy 默认将 fft 应用于最后一个轴(行)

希望有帮助。

于 2013-05-15T22:59:46.200 回答