return max(0.0, 0.5 * (1.0 + math.cos(math.pi * float(num_cycles) * 2.0 * progress)))
时间: 2023-12-24 15:02:41 浏览: 74
这段代码是一个余弦函数的实现,用于实现一个渐变效果。其中,num_cycles 表示余弦函数的周期数,progress 表示当前进度,取值范围为 [0,1]。当 progress 为 0 时,函数的返回值为 0;当 progress 为 1 时,函数的返回值为 1。当 progress 处于中间位置时,函数的返回值会在 0 和 1 之间不断变化,产生一个渐变的效果。
相关问题
#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 显示屏的具体型号和引脚配置进行相应的修改。
希望这个修改后的代码能够满足你的需求!如果你还有其他问题,请随时提问。
给下列程序添加注释:bool DijkstraExpansion::calculatePotentials(unsigned char* costs, double start_x, double start_y, double end_x, double end_y, int cycles, float* potential) { cells_visited_ = 0; // priority buffers threshold_ = lethal_cost_; currentBuffer_ = buffer1_; currentEnd_ = 0; nextBuffer_ = buffer2_; nextEnd_ = 0; overBuffer_ = buffer3_; overEnd_ = 0; memset(pending_, 0, ns_ * sizeof(bool)); std::fill(potential, potential + ns_, POT_HIGH); // set goal int k = toIndex(start_x, start_y); if(precise_) { double dx = start_x - (int)start_x, dy = start_y - (int)start_y; dx = floorf(dx * 100 + 0.5) / 100; dy = floorf(dy * 100 + 0.5) / 100; potential[k] = neutral_cost_ * 2 * dx * dy; potential[k+1] = neutral_cost_ * 2 * (1-dx)*dy; potential[k+nx_] = neutral_cost_*2*dx*(1-dy); potential[k+nx_+1] = neutral_cost_*2*(1-dx)*(1-dy);//*/ push_cur(k+2); push_cur(k-1); push_cur(k+nx_-1); push_cur(k+nx_+2); push_cur(k-nx_); push_cur(k-nx_+1); push_cur(k+nx_*2); push_cur(k+nx_*2+1); }else{ potential[k] = 0; push_cur(k+1); push_cur(k-1); push_cur(k-nx_); push_cur(k+nx_); }
// This function calculates the potentials of all the cells in the grid based on Dijkstra's algorithm.
// It takes in a 2D array of costs, the starting and ending coordinates, the number of iterations to perform, and an array to store the potentials.
bool DijkstraExpansion::calculatePotentials(unsigned char* costs, double start_x, double start_y, double end_x, double end_y, int cycles, float* potential) {
// Initialize variables
cells_visited_ = 0;
threshold_ = lethal_cost_;
currentBuffer_ = buffer1_;
currentEnd_ = 0;
nextBuffer_ = buffer2_;
nextEnd_ = 0;
overBuffer_ = buffer3_;
overEnd_ = 0;
memset(pending_, 0, ns_ * sizeof(bool));
std::fill(potential, potential + ns_, POT_HIGH);
// Set starting cell as goal cell
int k = toIndex(start_x, start_y);
// If precise mode is enabled, calculate potentials for each subcell
if(precise_) {
double dx = start_x - (int)start_x, dy = start_y - (int)start_y;
dx = floorf(dx * 100 + 0.5) / 100;
dy = floorf(dy * 100 + 0.5) / 100;
potential[k] = neutral_cost_ * 2 * dx * dy;
potential[k+1] = neutral_cost_ * 2 * (1-dx)*dy;
potential[k+nx_] = neutral_cost_*2*dx*(1-dy);
potential[k+nx_+1] = neutral_cost_*2*(1-dx)*(1-dy);
// Add surrounding cells to priority buffer
push_cur(k+2);
push_cur(k-1);
push_cur(k+nx_-1);
push_cur(k+nx_+2);
push_cur(k-nx_);
push_cur(k-nx_+1);
push_cur(k+nx_*2);
push_cur(k+nx_*2+1);
}
// If precise mode is disabled, calculate potentials for each cell in the grid
else {
potential[k] = 0;
push_cur(k+1);
push_cur(k-1);
push_cur(k-nx_);
push_cur(k+nx_);
}
}
阅读全文