void Plot_Wave(uint16_t *Data,uint16_t MAX,uint16_t x,uint16_t y,uint16_t Start) { // uint16_t Data_Max;//存放数组中的最大值 // uint16_t Ymax,Ymin; uint16_t TemData[200]; uint16_t i; float Ratio;//用来对数据进行归一化显示 if(MAX>200) { Ratio = (float)(200/MAX); } else {Ratio = 1;} for(i=0:i<200;i++) { if((uint16_t)(Ratio*Data[Start+i])>100) { TemData[i] = (uint16_t)(Ratio*Data[Start+i]); } else { TemData[i] = -(uint16_t)(Ratio*Data[Start+i]); } } LCD_SetBackColor(WHITE); ILI9341_Clear(0,0,360,240); Plot_Graph(MAX,x,y); LCD_SetTextColor(RED); for(i=0;i<199;i++) { ILI9341_DrawLine(x+100+i,y+TemData[i],x+101+i,y+TemData[i+1]); } }
时间: 2024-04-12 14:33:01 浏览: 103
这是一个绘制波形的函数,使用了ILI9341液晶屏和一个存储波形数据的数组。函数的输入参数包括Data数组、数组的最大值MAX、绘制波形的起始坐标(x, y)以及Start参数用于指定从Data数组的哪个位置开始绘制波形。
首先,函数会根据MAX值计算一个归一化比例Ratio。如果MAX大于200,Ratio将等于200/MAX,否则Ratio为1。
然后,函数会遍历长度为200的临时数组TemData,在每个位置上计算归一化后的数据,并存储到TemData数组中。如果归一化后的数据大于100,将存储正值,否则存储负值。
接下来,函数会使用LCD_SetBackColor函数设置背景颜色为白色,并清除整个屏幕。
然后,函数会调用Plot_Graph函数绘制一个坐标轴。
最后,函数会使用ILI9341_DrawLine函数在屏幕上绘制波形,通过连接相邻的点来实现。
请注意,该代码中有一些被注释掉的代码段,可能是之前的尝试或者备用代码。
相关问题
#include "arduinoFFT.h" arduinoFFT FFT = arduinoFFT(); const uint16_t samples = 64; const double signalFrequency = 1000; const double samplingFrequency = 5000; const uint8_t amplitude = 100; double vReal[samples]; double vImag[samples]; #define SCL_INDEX 0x00 #define SCL_TIME 0x01 #define SCL_FREQUENCY 0x02 #define SCL_PLOT 0x03 void setup() { Serial.begin(115200); while(!Serial); Serial.println("Ready"); } void loop() { double cycles = (((samples-1) * signalFrequency) / samplingFrequency); for (uint16_t i = 0; i < samples; i++) { vReal[i] = int8_t((amplitude * (sin((i * (twoPi * cycles)) / samples))) / 2.0); vImag[i] = 0.0; } Serial.println("Data:"); PrintVector(vReal, samples, SCL_TIME); FFT.Windowing(vReal, samples, FFT_WIN_TYP_HAMMING, FFT_FORWARD); /* Weigh data */ Serial.println("Weighed data:"); PrintVector(vReal, samples, SCL_TIME); FFT.Compute(vReal, vImag, samples, FFT_FORWARD); /* Compute FFT */ Serial.println("Computed Real values:"); PrintVector(vReal, samples, SCL_INDEX); Serial.println("Computed Imaginary values:"); PrintVector(vImag, samples, SCL_INDEX); FFT.ComplexToMagnitude(vReal, vImag, samples); /* Compute magnitudes */ Serial.println("Computed magnitudes:"); PrintVector(vReal, (samples >> 1), SCL_FREQUENCY); double x; double v; FFT.MajorPeak(vReal, samples, samplingFrequency, &x, &v); Serial.print(x, 6); Serial.print(", "); Serial.println(v, 6); while(1); } void PrintVector(double *vData, uint16_t bufferSize, uint8_t scaleType) { for (uint16_t i = 0; i < bufferSize; i++) { double abscissa; switch (scaleType) { case SCL_INDEX: abscissa = (i * 1.0); break; case SCL_TIME: abscissa = ((i * 1.0) / samplingFrequency); break; case SCL_FREQUENCY: abscissa = ((i * 1.0 * samplingFrequency) / samples); break; } Serial.print(abscissa, 6); if(scaleType==SCL_FREQUENCY) Serial.print("Hz"); Serial.print(" "); Serial.println(vData[i], 4); } Serial.println(); } 根据此历程中fft转换的方法修改oled代码
根据你提供的代码,你想要修改 OLED 显示代码来显示 FFT 转换的结果。我已经修改了代码,添加了 OLED 显示的相关部分。请注意,这里假设你已经连接了 OLED 显示屏并正确配置了 Arduino 板。
```c++
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "arduinoFFT.h"
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
arduinoFFT FFT = arduinoFFT();
const uint16_t samples = 64;
const double signalFrequency = 1000;
const double samplingFrequency = 5000;
const uint8_t amplitude = 100;
double vReal[samples];
double vImag[samples];
void setup()
{
Serial.begin(115200);
while (!Serial)
;
Serial.println("Ready");
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
}
void loop()
{
double cycles = (((samples - 1) * signalFrequency) / samplingFrequency);
for (uint16_t i = 0; i < samples; i++)
{
vReal[i] = int8_t((amplitude * (sin((i * (twoPi * cycles)) / samples))) / 2.0);
vImag[i] = 0.0;
}
display.clearDisplay();
Serial.println("Data:");
PrintVector(vReal, samples, SCL_TIME);
FFT.Windowing(vReal, samples, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
Serial.println("Weighed data:");
PrintVector(vReal, samples, SCL_TIME);
FFT.Compute(vReal, vImag, samples, FFT_FORWARD);
Serial.println("Computed Real values:");
PrintVector(vReal, samples, SCL_INDEX);
Serial.println("Computed Imaginary values:");
PrintVector(vImag, samples, SCL_INDEX);
FFT.ComplexToMagnitude(vReal, vImag, samples);
Serial.println("Computed magnitudes:");
PrintVector(vReal, (samples >> 1), SCL_FREQUENCY);
double x;
double v;
FFT.MajorPeak(vReal, samples, samplingFrequency, &x, &v);
Serial.print(x, 6);
Serial.print(", ");
Serial.println(v, 6);
// 显示 FFT 转换的结果
display.setCursor(0, 0);
display.print("Frequency: ");
display.println(x, 6);
display.print("Magnitude: ");
display.println(v, 6);
display.display();
while (1)
;
}
void PrintVector(double *vData, uint16_t bufferSize, uint8_t scaleType)
{
for (uint16_t i = 0; i < bufferSize; i++)
{
double abscissa;
switch (scaleType)
{
case SCL_INDEX:
abscissa = (i * 1.0);
break;
case SCL_TIME:
abscissa = ((i * 1.0) / samplingFrequency);
break;
case SCL_FREQUENCY:
abscissa = ((i * 1.0 * samplingFrequency) / samples);
break;
}
Serial.print(abscissa, 6);
if (scaleType == SCL_FREQUENCY)
Serial.print("Hz");
Serial.print(" ");
Serial.println(vData[i], 4);
}
Serial.println();
}
```
在这个修改后的代码中,我添加了 OLED 显示的相关部分。在 `loop` 函数中,我们在每次进行 FFT 转换后,通过 `display` 对象显示频率和幅值的结果。
请确保你已经连接了 OLED 显示屏,并将其引脚与 Arduino 板正确连接。如果需要,你可以根据 OLED 显示屏的具体型号和引脚配置进行相应的修改。
希望这个修改后的代码能够满足你的需求!如果你还有其他问题,请随时提问。
翻译这段程序并自行赋值调用:import matplotlib.pyplot as plt import numpy as np import sklearn import sklearn.datasets import sklearn.linear_model def plot_decision_boundary(model, X, y): # Set min and max values and give it some padding x_min, x_max = X[0, :].min() - 1, X[0, :].max() + 1 y_min, y_max = X[1, :].min() - 1, X[1, :].max() + 1 h = 0.01 # Generate a grid of points with distance h between them xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) # Predict the function value for the whole grid Z = model(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) # Plot the contour and training examples plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral) plt.ylabel('x2') plt.xlabel('x1') plt.scatter(X[0, :], X[1, :], c=y, cmap=plt.cm.Spectral) def sigmoid(x): s = 1/(1+np.exp(-x)) return s def load_planar_dataset(): np.random.seed(1) m = 400 # number of examples N = int(m/2) # number of points per class print(np.random.randn(N)) D = 2 # dimensionality X = np.zeros((m,D)) # data matrix where each row is a single example Y = np.zeros((m,1), dtype='uint8') # labels vector (0 for red, 1 for blue) a = 4 # maximum ray of the flower for j in range(2): ix = range(Nj,N(j+1)) t = np.linspace(j3.12,(j+1)3.12,N) + np.random.randn(N)0.2 # theta r = anp.sin(4t) + np.random.randn(N)0.2 # radius X[ix] = np.c_[rnp.sin(t), rnp.cos(t)] Y[ix] = j X = X.T Y = Y.T return X, Y def load_extra_datasets(): N = 200 noisy_circles = sklearn.datasets.make_circles(n_samples=N, factor=.5, noise=.3) noisy_moons = sklearn.datasets.make_moons(n_samples=N, noise=.2) blobs = sklearn.datasets.make_blobs(n_samples=N, random_state=5, n_features=2, centers=6) gaussian_quantiles = sklearn.datasets.make_gaussian_quantiles(mean=None, cov=0.5, n_samples=N, n_features=2, n_classes=2, shuffle=True, random_state=None) no_structure = np.random.rand(N, 2), np.random.rand(N, 2) return noisy_circles, noisy_moons, blobs, gaussian_quantiles, no_structure
这段程序是一个分类模型的辅助函数,包括了绘制决策边界、sigmoid函数和加载数据集的函数。具体实现如下:
```python
import matplotlib.pyplot as plt
import numpy as np
import sklearn
import sklearn.datasets
import sklearn.linear_model
def plot_decision_boundary(model, X, y):
# 设置最小值和最大值,并给它们一些填充
x_min, x_max = X[0, :].min() - 1, X[0, :].max() + 1
y_min, y_max = X[1, :].min() - 1, X[1, :].max() + 1
h = 0.01
# 生成一个网格,网格中点的距离为h
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
# 对整个网格预测函数值
Z = model(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
# 绘制轮廓和训练样本
plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral)
plt.ylabel('x2')
plt.xlabel('x1')
plt.scatter(X[0, :], X[1, :], c=y, cmap=plt.cm.Spectral)
def sigmoid(x):
s = 1 / (1 + np.exp(-x))
return s
def load_planar_dataset():
np.random.seed(1)
m = 400 # 样本数量
N = int(m / 2) # 每个类的样本数量
# 生成数据集
D = 2 # 特征维度
X = np.zeros((m, D)) # 特征矩阵
Y = np.zeros((m, 1), dtype='uint8') # 标签向量
a = 4 # 花的最大半径
for j in range(2):
ix = range(N*j, N*(j+1))
t = np.linspace(j*3.12, (j+1)*3.12, N) + np.random.randn(N)*0.2 # theta
r = a*np.sin(4*t) + np.random.randn(N)*0.2 # radius
X[ix] = np.c_[r*np.sin(t), r*np.cos(t)]
Y[ix] = j
X = X.T
Y = Y.T
return X, Y
def load_extra_datasets():
N = 200
noisy_circles = sklearn.datasets.make_circles(n_samples=N, factor=.5, noise=.3)
noisy_moons = sklearn.datasets.make_moons(n_samples=N, noise=.2)
blobs = sklearn.datasets.make_blobs(n_samples=N, random_state=5, n_features=2, centers=6)
gaussian_quantiles = sklearn.datasets.make_gaussian_quantiles(mean=None, cov=0.5, n_samples=N, n_features=2, n_classes=2, shuffle=True, random_state=None)
no_structure = np.random.rand(N, 2), np.random.rand(N, 2)
return noisy_circles, noisy_moons, blobs, gaussian_quantiles, no_structure
```
这段程序中包含了以下函数:
- `plot_decision_boundary(model, X, y)`:绘制分类模型的决策边界,其中`model`是分类模型,`X`是特征矩阵,`y`是标签向量。
- `sigmoid(x)`:实现sigmoid函数。
- `load_planar_dataset()`:加载一个二维的花瓣数据集。
- `load_extra_datasets()`:加载五个其他数据集。
阅读全文