-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ignitor.cpp
108 lines (86 loc) · 3.73 KB
/
Ignitor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "mbed.h"
#include <cmath>
#include <vector>
// Constants for Goertzel algorithm
const float f_target = 100; // Target frequency [Hz]
const float fs = 1000; // Sampling frequency [Hz]
const int N = 100; // Number of samples for Goertzel algorithm
// Goertzel algorithm variables
float r, s, w; // Goertzel coefficients
float x_prev1 = 0; // Previous output x(n-1)
float x_prev2 = 0; // Previous output x(n-2)
float amplitude = 0; // Amplitude of the target frequency
float phase = 0; // Phase of the target frequency
// Data buffer for the moving window
std::vector<float> data_window;
// Simulated GPIO setup function (replace this with your actual setup logic)
void setupGPIO(int pin, bool mode) {
std::cout << "Setting up GPIO pin " << pin << " in mode " << mode << std::endl;
}
// Simulated GPIO read function (replace this with your actual reading logic)
bool readGPIO(int pin) {
std::cout << "Reading from GPIO pin " << pin << std::endl;
return true; // Replace this with the actual reading logic
}
float readFlowSensor(int pin) {
setupGPIO(pin, false); // Replace false with the actual mode for input
int flowRate = 0;
// Simulate periodic readings (for demonstration purposes)
for (int i = 0; i < 5; ++i) { // Simulate 5 readings (adjust as needed)
// Assuming you have some delay to simulate periodic readings
std::this_thread::sleep_for(std::chrono::seconds(1));
// Simulated pulse detection (replace this with actual logic)
if (readGPIO(pin)) {
flowRate++;
}
}
return flowRate;
}
// Function to calculate the Goertzel coefficients
void calcGoertzelCoefficients() {
int k = static_cast<int>(N * f_target / fs);
float theta = 2 * 3.14159 * k / N;
r = 2.0 * cos(theta);
s = sin(theta);
w = 2.0 * cos(2 * theta);
}
// Function to perform the Goertzel algorithm and obtain amplitude and phase data
void performGoertzelAlgorithm(float x_curr) {
float x_temp = x_curr + r * x_prev1 - x_prev2;
x_prev2 = x_prev1;
x_prev1 = x_temp;
// Calculate magnitude squared (amplitude squared) from Goertzel output
float x_result = x_prev2 * x_prev2 + x_prev1 * x_prev1 - r * x_prev1 * x_prev2;
// Convert magnitude squared to amplitude and phase
amplitude = sqrt(x_result);
phase = atan2(x_prev1, x_prev2);
}
int main() {
// Initializing and configuring microcontroller peripherals and GPIO pins
calcGoertzelCoefficients();
while (true) {
// Read the input signal from the flow sensor
float input_signal = readFlowSensor(); // Replace readFlowSensor() with actual function to read from flow sensor
// Add the new data point to the data_window
data_window.push_back(input_signal);
// If the size of data_window exceeds N, remove the oldest data point
if (data_window.size() > N) {
data_window.erase(data_window.begin());
}
// Perform Goertzel algorithm on the data in the data_window
performGoertzelAlgorithm(input_signal);
// Fourier series approximation using Goertzel-derived amplitude and phase data
float t = 0; // Time variable for generating the Fourier series
float sum = 0; // Sum of the Fourier series
for (int i = 0; i < data_window.size(); i++) {
// Generate the ith harmonic component and add it to the sum
sum += amplitude * cos(2 * 3.14159 * f_target * t / fs + phase);
t += 1.0 / fs;
}
// Additional code for using the Fourier series approximation (sum)
// For example, you can use 'sum' to represent the approximated signal.
// ...
// Additional code for the main loop
// ...
}
}