如何将Arduino信号从Python转换为快速傅立叶变换?

时间:2019-11-22 01:20:01

标签: matplotlib arduino

我现在正在尝试将信号转换为Python中的快速傅立叶变换并绘制图形。我在这里对伦有问题。我怎样才能解决这个问题?还有人对转换快速傅立叶变换有其他想法吗?

发生异常:TypeError

“方法”类型的对象没有len()

那是我的问题。

from PyQt5.QtWidgets import*
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import random
from PyQt5 import QtCore, QtGui, QtWidgets
import datetime
import serial
import time
import random
import numpy as np
from matplotlib import animation
from collections import deque
import threading


x = 0
value = [0]

ser = serial.Serial('com5', 9600)

class scope : 

    def data(self) :
        if ser.readable() :
            time.sleep(0.01)
            reciving = ser.readline(ser.inWaiting())           
            str = reciving.decode()
            if len(str) > 0  :         
                if str[:1] == 'X' :
                    value[0] = str[1:]
                    #print(float(value[5]))
                    time.sleep(0.5)
                    x = float(value[0])
        return x

s = scope()

n = len(s.data)

Ts = 0.01
Fs = 1/Ts                     
                    # length of the signal
k = np.arange(n)
T = n/Fs
freq = k/T                  # two sides frequency range
freq = freq[range(int(n/2))]            # one side frequency range

Y = np.fft.fft(x)/n                 # fft computing and normalization
Y = Y[range(int(n/2))]

fig, ax = plt.subplots(2, 1)

ax.plot(freq, abs(Y), 'r', linestyle=' ', marker='^') 
ax.set_xlabel('Freq (Hz)')
ax.set_ylabel('|Y(freq)|')
#3ax.vlines(freq, [0], abs(Y))
ax.grid(True)

t = threading.Thread(target= s.data)
t.daemon = True
t.start()

plt.show()

0 个答案:

没有答案