IEC61499 programming (5)-FFT transform function block

FFT is a method of converting the time domain into the frequency domain, and is often used in data processing, such as vibration analysis. In order to test the Open IEC61499 function block library we are developing. An application program is written, which consists of the following functional blocks

E_CYCLE cycle event generator

E_DIVID_N N divider . Generate sampling frequency

FB_WAVE waveform generator

  At present, this function block can generate Sin, Cos and sawtooth waves. The main parameters are as follows.

Type Waveform type 0-Sin, 1-Cos, 2-Sawtooth

Sample sampling frequency

A-Amplitude

F-frequency

P-phase shift

V-level shift

FB_FFT_W Fast Fourier Transform function block

P-FFT points

DIN is data input

FFTOUT-FFT data output.

FB_BUF_256

256 REAL value buffer function blocks. Form 256 REALs into 256 arrays. Send via UDP Publish. In this way, it is more efficient to send 1024 bytes over the network.

If you have the basic knowledge of IEC61499 function blocks, you will find the efficiency and convenience brought by OpenIEC61499.

Python receiver

import socket
import struct
from matplotlib import pyplot as plt
def bytesToFloat(h1,h2,h3,h4): 
    ba = bytearray()
    ba.append(h1) 
    ba.append(h2) 
    ba.append(h3) 
    ba.append(h4) 
    return struct.unpack("!f",ba)[0]
HOST = '192.168.31.108'
PORT = 8888
BUFSIZ =1025
ADDR = (HOST,PORT)
udpSerSock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
udpSerSock.bind(ADDR)

x=[]
y=[]
for i in range(255):
    x.append(i)
    y.append(0)
plt.show()
while True:
    data, addr = udpSerSock.recvfrom(BUFSIZ)
    print ('Received bytes%d\n' ,len(data))
    
    for i in range(255):
       y[i]=bytesToFloat(data[i*4+4],data[i*4+3],data[i*4+2],data[i*4+1])
    max=0
    delta=0
    for i in range(128):
        if (y[i]>max):
              delta=i
              max=y[i]
    f=delta*1000/256        
    plt.clf()
    plt.title("frequncy="+str(f)+"Hz")
    plt.plot(x,y)
    plt.pause(0.05)

The results show that 

 

 

Guess you like

Origin blog.csdn.net/yaojiawan/article/details/106800857