Compare commits

...

3 commits

Author SHA1 Message Date
Kai Lauterbach
a11a75a2c5 Added ChatGPT suggested fixed for frequency calculation. 2024-02-09 21:43:40 +01:00
Kai Lauterbach
7633963ea7 Code formatted 2024-02-09 21:31:21 +01:00
Kai Lauterbach
c099bb3a53 TODO comment added 2024-02-09 21:31:11 +01:00
3 changed files with 155 additions and 153 deletions

View file

@ -210,6 +210,7 @@ void core1_task(void *pvParameters)
{ {
if (stop_change) if (stop_change)
{ {
// TODO the enable function causes an esp32 deadlock
//i2s_adc_enable(I2S_NUM_0); //i2s_adc_enable(I2S_NUM_0);
i2s_zero_dma_buffer(I2S_NUM_0); i2s_zero_dma_buffer(I2S_NUM_0);
stop_change = false; stop_change = false;

View file

@ -69,98 +69,102 @@ void trigger_freq_analog(uint16_t *i2s_buffer,
uint32_t *pt_trigger0, uint32_t *pt_trigger0,
uint32_t *pt_trigger1) uint32_t *pt_trigger1)
{ {
float freq = 0; float freq = 0;
float period = 0; float period = 0;
bool signal_side = false; bool signal_side = false;
uint32_t trigger_count = 0; uint32_t trigger_count = 0;
uint32_t trigger_num = 10; uint32_t trigger_num = 10;
uint32_t trigger_temp[trigger_num] = {0}; uint32_t trigger_temp[trigger_num] = {0};
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)
{ {
signal_side = false; bool current_signal_side = (to_voltage(i2s_buffer[i]) > mean);
}
else if (!signal_side && i2s_buffer[i] > wave_center + (max_v - wave_center) * 0.2)
{
freq++;
if (trigger_count < trigger_num)
{
trigger_temp[trigger_count] = i;
trigger_count++;
}
signal_side = true;
}
}
// frequency calculation if (previous_signal_side && !current_signal_side)
if (trigger_count < 2) {
{ freq++;
trigger_temp[0] = 0; if (trigger_count < trigger_num)
trigger_index = 0; {
freq = 0; trigger_temp[trigger_count] = i;
period = 0; trigger_count++;
} }
else } else if (!previous_signal_side && current_signal_side)
{ {
freq++;
if (trigger_count < trigger_num)
{
trigger_temp[trigger_count] = i;
trigger_count++;
}
}
// simple frequency calculation fair enough for frequencies over 2khz (20hz resolution) previous_signal_side = current_signal_side;
freq = freq * 1000 / 50;
period = (float)(sample_rate * 1000.0) / freq; // us
// from 2000 to 80 hz -> uses mean of the periods for precision
if (freq < 2000 && freq > 80)
{
period = 0;
for (uint32_t i = 1; i < trigger_count; i++)
{
period += trigger_temp[i] - trigger_temp[i - 1];
}
period /= (trigger_count - 1);
freq = sample_rate * 1000 / period;
} }
// under 80hz, single period for frequency calculation // frequency calculation
else if (trigger_count > 1 && freq <= 80) if (trigger_count < 2)
{ {
period = trigger_temp[1] - trigger_temp[0]; trigger_temp[0] = 0;
freq = sample_rate * 1000 / period; trigger_index = 0;
freq = 0;
period = 0;
} }
} else
{
// setting triggers offset and getting second trigger for debug cursor on drawn_channel1 // simple frequency calculation fair enough for frequencies over 2khz (20hz resolution)
/* freq = freq * 1000 / 50;
period = (float)(sample_rate * 1000.0) / freq; // us
// from 2000 to 80 hz -> uses mean of the periods for precision
if (freq < 2000 && freq > 80)
{
period = 0;
for (uint32_t i = 1; i < trigger_count; i++)
{
period += trigger_temp[i] - trigger_temp[i - 1];
}
period /= (trigger_count - 1);
freq = sample_rate * 1000 / period;
}
// under 80hz, single period for frequency calculation
else if (trigger_count > 1 && freq <= 80)
{
period = trigger_temp[1] - trigger_temp[0];
freq = sample_rate * 1000 / period;
}
}
// setting triggers offset and getting second trigger for debug cursor on drawn_channel1
/*
The trigger function uses a rise porcentage (5%) obove the mean, thus, The trigger function uses a rise porcentage (5%) obove the mean, thus,
the real waveform starting point is some datapoints back. the real waveform starting point is some datapoints back.
The resulting trigger gets a negative offset of 5% of the calculated period The resulting trigger gets a negative offset of 5% of the calculated period
*/ */
uint32_t trigger2 = 0; uint32_t trigger2 = 0;
if (trigger_temp[0] - period * 0.05 > 0 && trigger_count > 1) if (trigger_temp[0] - period * 0.05 > 0 && trigger_count > 1)
{ {
trigger_index = trigger_temp[0] - period * 0.05; trigger_index = trigger_temp[0] - period * 0.05;
trigger2 = trigger_temp[1] - period * 0.05; trigger2 = trigger_temp[1] - period * 0.05;
} }
else if (trigger_count > 2) else if (trigger_count > 2)
{ {
trigger_index = trigger_temp[1] - period * 0.05; trigger_index = trigger_temp[1] - period * 0.05;
if (trigger_count > 2) if (trigger_count > 2)
trigger2 = trigger_temp[2] - period * 0.05; trigger2 = trigger_temp[2] - period * 0.05;
} }
pt_trigger0[0] = trigger_index; pt_trigger0[0] = trigger_index;
pt_trigger1[0] = trigger2; pt_trigger1[0] = trigger2;
pt_freq[0] = freq; pt_freq[0] = freq;
pt_period[0] = period; pt_period[0] = period;
} }
void trigger_freq_digital(uint16_t *i2s_buffer, void trigger_freq_digital(uint16_t *i2s_buffer,
@ -172,89 +176,84 @@ 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 period = 0;
bool signal_side = false;
uint32_t trigger_count = 0;
uint32_t trigger_num = 10;
uint32_t trigger_temp[trigger_num] = {0};
uint32_t trigger_index = 0;
float freq = 0; // get initial signal relative to the mean
float period = 0; bool previous_signal_side = (to_voltage(i2s_buffer[0]) > mean);
bool signal_side = false;
uint32_t trigger_count = 0;
uint32_t trigger_num = 10;
uint32_t trigger_temp[trigger_num] = {0};
uint32_t trigger_index = 0;
// get initial signal relative to the mean // waveform repetitions calculation + get triggers time
if (to_voltage(i2s_buffer[0]) > mean) uint32_t wave_center = (max_v + min_v) / 2;
{ bool normal_high = (mean > to_voltage(wave_center)) ? true : false;
signal_side = true; if (max_v - min_v > 4095 * (0.4 / 3.3))
}
// waveform repetitions calculation + get triggers time
uint32_t wave_center = (max_v + min_v) / 2;
bool normal_high = (mean > to_voltage(wave_center)) ? true : false;
if (max_v - min_v > 4095 * (0.4 / 3.3))
{
for (uint32_t i = 1; i < BUFF_SIZE; i++)
{ {
if (signal_side && i2s_buffer[i] < wave_center - (wave_center - min_v) * 0.2) for (uint32_t i = 1; i < BUFF_SIZE; i++)
{
// signal was high, fell -> trigger if normal high
if (trigger_count < trigger_num && normal_high)
{ {
trigger_temp[trigger_count] = i; bool current_signal_side = (to_voltage(i2s_buffer[i]) > mean);
trigger_count++;
if (previous_signal_side && !current_signal_side)
{
// signal was high, fell -> trigger if normal high
if (trigger_count < trigger_num && normal_high)
{
trigger_temp[trigger_count] = i;
trigger_count++;
}
}
else if (!previous_signal_side && current_signal_side)
{
// signal was low, rose -> trigger if normal low
if (trigger_count < trigger_num && !normal_high)
{
trigger_temp[trigger_count] = i;
trigger_count++;
}
}
previous_signal_side = current_signal_side;
} }
signal_side = false; // frequency calculation
} if (trigger_count > 1)
else if (!signal_side && i2s_buffer[i] > wave_center + (max_v - wave_center) * 0.2)
{
freq++;
// signal was low, rose -> trigger if normal low
if (trigger_count < trigger_num && !normal_high)
{ {
trigger_temp[trigger_count] = i; // simple frequency calculation fair enough for frequencies over 2khz (20hz resolution)
trigger_count++; freq = freq * 1000 / 50;
period = (float)(sample_rate * 1000.0) / freq; // us
// from 2000 to 80 hz -> uses mean of the periods for precision
if (freq < 2000 && freq > 80)
{
period = 0;
for (uint32_t i = 1; i < trigger_count; i++)
{
period += trigger_temp[i] - trigger_temp[i - 1];
}
period /= (trigger_count - 1);
freq = sample_rate * 1000 / period;
}
// under 80hz, single period for frequency calculation
else if (trigger_count > 1 && freq <= 80)
{
period = trigger_temp[1] - trigger_temp[0];
freq = sample_rate * 1000 / period;
}
} }
signal_side = true; trigger_index = trigger_temp[0];
}
if (trigger_index > 10)
trigger_index -= 10;
else
trigger_index = 0;
} }
freq = freq * 1000 / 50; pt_trigger0[0] = trigger_index;
period = (float)(sample_rate * 1000.0) / freq; // us pt_freq[0] = freq;
pt_period[0] = period;
if (trigger_count > 1)
{
// from 2000 to 80 hz -> uses mean of the periods for precision
if (freq < 2000 && freq > 80)
{
period = 0;
for (uint32_t i = 1; i < trigger_count; i++)
{
period += trigger_temp[i] - trigger_temp[i - 1];
}
period /= (trigger_count - 1);
freq = sample_rate * 1000 / period;
}
// under 80hz, single period for frequency calculation
else if (trigger_count > 1 && freq <= 80)
{
period = trigger_temp[1] - trigger_temp[0];
freq = sample_rate * 1000 / period;
}
}
trigger_index = trigger_temp[0];
if (trigger_index > 10)
trigger_index -= 10;
else
trigger_index = 0;
}
pt_trigger0[0] = trigger_index;
pt_freq[0] = freq;
pt_period[0] = period;
} }

View file

@ -32,9 +32,11 @@ void ADC_Sampling(uint16_t *i2s_buff){
} }
*/ */
void ADC_Sampling(uint16_t *i2s_buff){ void ADC_Sampling(uint16_t *i2s_buff){
size_t bytes_read; for (int i = 0; i < B_MULT; i++) { size_t bytes_read;
for (int i = 0; i < B_MULT; i++) {
i2s_read(I2S_NUM_0, (void*)&i2s_buff[i * NUM_SAMPLES], NUM_SAMPLES * sizeof(uint16_t), &bytes_read, portMAX_DELAY); i2s_read(I2S_NUM_0, (void*)&i2s_buff[i * NUM_SAMPLES], NUM_SAMPLES * sizeof(uint16_t), &bytes_read, portMAX_DELAY);
for(size_t ix = 0; ix < bytes_read/2; ix++) i2s_buff[(i * NUM_SAMPLES) + ix] &= 0x0FFF; // 16bit to 12bit conversion  for(size_t ix = 0; ix < bytes_read/2; ix++)
i2s_buff[(i * NUM_SAMPLES) + ix] &= 0x0FFF; // 16bit to 12bit conversion 
} }
} }