FFT (plot)

 1'''
 2FFT - fourier_transform.py.
 3
 4This example demonstrates how to plot FFT signal
 5For PC relative plotting code, please refer https://github.com/WPC-Systems-Ltd/WPC_Stand-alone_Python_release/blob/main/examples/PC/TCP_client_with_plot_FFT.py
 6
 7Step1: Generate FFT signal
 8
 9Step2: Convert float to U8 list
10
11Step3: Connect to Wifi
12
13Step4: Send data via TCP server
14
15Step5: Plot FFT signal in PC
16
17For other examples please check:
18    https://github.com/WPC-Systems-Ltd/WPC_Stand-alone_Python_release/tree/main/examples
19
20Copyright (c) 2024 WPC Systems Ltd.
21All rights reserved.
22'''
23
24from ulab import numpy as np
25import pywpc
26
27## Parameters
28Fs = 256                ## sampling rate
29Ts = 1.0/Fs             ## sampling interval
30t = np.arange(0, 1, Ts) ## time vector
31frequency = 50          ## frequency of the signal
32
33## Sine signal
34signal = np.sin(2*np.pi*frequency*t)
35
36## Apply the FFT on the signal
37fourier_real, fourier_imaginary = np.fft.fft(signal)
38power_spectrum = fourier_real**2 + fourier_imaginary**2
39
40## Create list
41u8_list = []
42for i in power_spectrum:
43    u8_list.extend(pywpc.Sys_convertF32To4U8(i))
44
45## Connect to Wifi
46
47## Open TCP server
48pywpc.TCPServer_open(5566)
49
50## Send u8 list data
51pywpc.TCPServer_writeU8List(u8_list)
52
53## Close TCP server
54pywpc.TCPServer_close()