Added ChatGPT suggested fixed for frequency calculation.
This commit is contained in:
parent
7633963ea7
commit
a11a75a2c5
1 changed files with 150 additions and 151 deletions
|
@ -78,20 +78,15 @@ void trigger_freq_analog(uint16_t *i2s_buffer,
|
||||||
uint32_t trigger_index = 0;
|
uint32_t trigger_index = 0;
|
||||||
|
|
||||||
// get initial signal relative to the mean
|
// get initial signal relative to the mean
|
||||||
if (to_voltage(i2s_buffer[0]) > mean)
|
bool previous_signal_side = (to_voltage(i2s_buffer[0]) > mean);
|
||||||
{
|
|
||||||
signal_side = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// waveform repetitions calculation + get triggers time
|
// waveform repetitions calculation + get triggers time
|
||||||
uint32_t wave_center = (max_v + min_v) / 2;
|
uint32_t wave_center = (max_v + min_v) / 2;
|
||||||
for (uint32_t i = 1; i < BUFF_SIZE; i++)
|
for (uint32_t i = 1; i < BUFF_SIZE; i++)
|
||||||
{
|
{
|
||||||
if (signal_side && i2s_buffer[i] < wave_center - (wave_center - min_v) * 0.2)
|
bool current_signal_side = (to_voltage(i2s_buffer[i]) > mean);
|
||||||
{
|
|
||||||
signal_side = false;
|
if (previous_signal_side && !current_signal_side)
|
||||||
}
|
|
||||||
else if (!signal_side && i2s_buffer[i] > wave_center + (max_v - wave_center) * 0.2)
|
|
||||||
{
|
{
|
||||||
freq++;
|
freq++;
|
||||||
if (trigger_count < trigger_num)
|
if (trigger_count < trigger_num)
|
||||||
|
@ -99,10 +94,19 @@ void trigger_freq_analog(uint16_t *i2s_buffer,
|
||||||
trigger_temp[trigger_count] = i;
|
trigger_temp[trigger_count] = i;
|
||||||
trigger_count++;
|
trigger_count++;
|
||||||
}
|
}
|
||||||
signal_side = true;
|
} else if (!previous_signal_side && current_signal_side)
|
||||||
|
{
|
||||||
|
freq++;
|
||||||
|
if (trigger_count < trigger_num)
|
||||||
|
{
|
||||||
|
trigger_temp[trigger_count] = i;
|
||||||
|
trigger_count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
previous_signal_side = current_signal_side;
|
||||||
|
}
|
||||||
|
|
||||||
// frequency calculation
|
// frequency calculation
|
||||||
if (trigger_count < 2)
|
if (trigger_count < 2)
|
||||||
{
|
{
|
||||||
|
@ -172,7 +176,6 @@ void trigger_freq_digital(uint16_t *i2s_buffer,
|
||||||
float *pt_period,
|
float *pt_period,
|
||||||
uint32_t *pt_trigger0)
|
uint32_t *pt_trigger0)
|
||||||
{
|
{
|
||||||
|
|
||||||
float freq = 0;
|
float freq = 0;
|
||||||
float period = 0;
|
float period = 0;
|
||||||
bool signal_side = false;
|
bool signal_side = false;
|
||||||
|
@ -182,10 +185,7 @@ void trigger_freq_digital(uint16_t *i2s_buffer,
|
||||||
uint32_t trigger_index = 0;
|
uint32_t trigger_index = 0;
|
||||||
|
|
||||||
// get initial signal relative to the mean
|
// get initial signal relative to the mean
|
||||||
if (to_voltage(i2s_buffer[0]) > mean)
|
bool previous_signal_side = (to_voltage(i2s_buffer[0]) > mean);
|
||||||
{
|
|
||||||
signal_side = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// waveform repetitions calculation + get triggers time
|
// waveform repetitions calculation + get triggers time
|
||||||
uint32_t wave_center = (max_v + min_v) / 2;
|
uint32_t wave_center = (max_v + min_v) / 2;
|
||||||
|
@ -194,38 +194,37 @@ void trigger_freq_digital(uint16_t *i2s_buffer,
|
||||||
{
|
{
|
||||||
for (uint32_t i = 1; i < BUFF_SIZE; i++)
|
for (uint32_t i = 1; i < BUFF_SIZE; i++)
|
||||||
{
|
{
|
||||||
if (signal_side && i2s_buffer[i] < wave_center - (wave_center - min_v) * 0.2)
|
bool current_signal_side = (to_voltage(i2s_buffer[i]) > mean);
|
||||||
{
|
|
||||||
|
|
||||||
|
if (previous_signal_side && !current_signal_side)
|
||||||
|
{
|
||||||
// signal was high, fell -> trigger if normal high
|
// signal was high, fell -> trigger if normal high
|
||||||
if (trigger_count < trigger_num && normal_high)
|
if (trigger_count < trigger_num && normal_high)
|
||||||
{
|
{
|
||||||
trigger_temp[trigger_count] = i;
|
trigger_temp[trigger_count] = i;
|
||||||
trigger_count++;
|
trigger_count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
signal_side = false;
|
|
||||||
}
|
}
|
||||||
else if (!signal_side && i2s_buffer[i] > wave_center + (max_v - wave_center) * 0.2)
|
else if (!previous_signal_side && current_signal_side)
|
||||||
{
|
{
|
||||||
freq++;
|
|
||||||
|
|
||||||
// signal was low, rose -> trigger if normal low
|
// signal was low, rose -> trigger if normal low
|
||||||
if (trigger_count < trigger_num && !normal_high)
|
if (trigger_count < trigger_num && !normal_high)
|
||||||
{
|
{
|
||||||
trigger_temp[trigger_count] = i;
|
trigger_temp[trigger_count] = i;
|
||||||
trigger_count++;
|
trigger_count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
signal_side = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
previous_signal_side = current_signal_side;
|
||||||
|
}
|
||||||
|
|
||||||
|
// frequency calculation
|
||||||
|
if (trigger_count > 1)
|
||||||
|
{
|
||||||
|
// simple frequency calculation fair enough for frequencies over 2khz (20hz resolution)
|
||||||
freq = freq * 1000 / 50;
|
freq = freq * 1000 / 50;
|
||||||
period = (float)(sample_rate * 1000.0) / freq; // us
|
period = (float)(sample_rate * 1000.0) / freq; // us
|
||||||
|
|
||||||
if (trigger_count > 1)
|
|
||||||
{
|
|
||||||
// from 2000 to 80 hz -> uses mean of the periods for precision
|
// from 2000 to 80 hz -> uses mean of the periods for precision
|
||||||
if (freq < 2000 && freq > 80)
|
if (freq < 2000 && freq > 80)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue