bands
This commit is contained in:
parent
6716e396a5
commit
cd9fd98012
2 changed files with 101 additions and 62 deletions
|
@ -62,10 +62,19 @@ treb: 43-255
|
|||
#define BUF_SIZE_BYTES (BUF_SIZE_SAMPLES * SAMPLE_BYTES)
|
||||
|
||||
#define FALLOFF(OLD_V, NEW_V) ((OLD_V >> 1) + (OLD_V >> 2) + (NEW_V >> 2))
|
||||
#define FALLOFF_F(OLD_V, NEW_V) ((OLD_V / 2.0f) + (OLD_V / 4.0f) + (NEW_V / 4.0f))
|
||||
#define ABS(T, v) ((v < 0 ? (((T)0) - v) : v))
|
||||
#define MAX(a, b) ((b > a) ? (b) : (a))
|
||||
#define MIN(a, b) ((b < a) ? (b) : (a))
|
||||
|
||||
#define SUM_BAND(IDX, BUF, START, END) \
|
||||
current_powers[IDX] = 0; \
|
||||
for (int i = START; i <= END; i++) \
|
||||
{ \
|
||||
current_powers[IDX] += BUF[i]; \
|
||||
} \
|
||||
current_powers[IDX] /= (END + 1 - START);
|
||||
|
||||
i2s_chan_handle_t rx_handle = NULL;
|
||||
TaskHandle_t proc_task = NULL;
|
||||
TaskHandle_t output_task = NULL;
|
||||
|
@ -76,11 +85,15 @@ __attribute__((aligned(16))) static SAMPLE_TYPE i2s_readraw_buff[BUF_SIZE_SAMPLE
|
|||
__attribute__((aligned(16))) static float fft_buffer[SAMPLE_COUNT * BUF_COUNT];
|
||||
static float wind[SAMPLE_COUNT];
|
||||
|
||||
uint32_t current_powers[BANDS_COUNT];
|
||||
uint32_t avg_powers[BANDS_COUNT];
|
||||
float current_powers[BANDS_COUNT];
|
||||
float avg_powers[BANDS_COUNT];
|
||||
|
||||
SAMPLE_TYPE floating_max;
|
||||
|
||||
// float recursive_add_f(float* buffer, int start, int end) {
|
||||
|
||||
// }
|
||||
|
||||
void app_init_mic(void)
|
||||
{
|
||||
i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_AUTO, I2S_ROLE_MASTER);
|
||||
|
@ -141,13 +154,13 @@ static void app_output_task(void *args)
|
|||
|
||||
uart_write_bytes(UART_NUM_0, sync_word, sizeof(sync_word));
|
||||
|
||||
xSemaphoreTake(output_mutex, portMAX_DELAY);
|
||||
uart_write_bytes(UART_NUM_0, &floating_max, sizeof(floating_max));
|
||||
uart_write_bytes(UART_NUM_0, fft_buf, sizeof(float) * SAMPLE_COUNT);
|
||||
uart_write_bytes(UART_NUM_0, avg_powers, sizeof(avg_powers));
|
||||
uart_write_bytes(UART_NUM_0, current_powers, sizeof(current_powers));
|
||||
xSemaphoreGive(output_mutex);
|
||||
|
||||
// xSemaphoreTake(output_mutex, portMAX_DELAY);
|
||||
// uart_write_bytes(UART_NUM_0, avg_powers, sizeof(avg_powers));
|
||||
// uart_write_bytes(UART_NUM_0, current_powers, sizeof(current_powers));
|
||||
// xSemaphoreGive(output_mutex);
|
||||
uart_write_bytes(UART_NUM_0, fft_buf, sizeof(float) * SAMPLE_COUNT / 2);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -176,8 +189,11 @@ static void app_process_task(void *args)
|
|||
{
|
||||
max = MAX(max, ABS(SAMPLE_TYPE, in_buf[i]));
|
||||
}
|
||||
|
||||
xSemaphoreTake(output_mutex, portMAX_DELAY);
|
||||
floating_max =
|
||||
MAX(10000000, (max > floating_max) ? max : FALLOFF(floating_max, FALLOFF(floating_max, max)));
|
||||
xSemaphoreGive(output_mutex);
|
||||
}
|
||||
|
||||
/* convert to floats for input to fft */
|
||||
|
@ -186,20 +202,24 @@ static void app_process_task(void *args)
|
|||
float *fft_buf = fft_buffer + next_fft_buf_idx * SAMPLE_COUNT;
|
||||
do
|
||||
{
|
||||
// *fft_buf++ = ((float)*in_buf++) / SAMPLE_MAX;
|
||||
*fft_buf++ = ((float)*in_buf++) / floating_max;
|
||||
*fft_buf++ = ((float)*in_buf++) / SAMPLE_MAX;
|
||||
// *fft_buf++ = ((float)*in_buf++) / floating_max;
|
||||
} while (in_buf < i2s_readraw_buff + (in_rx_buf_idx + 1) * BUF_SIZE_SAMPLES);
|
||||
}
|
||||
|
||||
/* do fft */
|
||||
float *fft_buf = fft_buffer + next_fft_buf_idx * SAMPLE_COUNT;
|
||||
{
|
||||
float *fft_buf = fft_buffer + next_fft_buf_idx * SAMPLE_COUNT;
|
||||
dsps_mul_f32(fft_buf, wind, fft_buf, SAMPLE_COUNT, 1, 1, 1);
|
||||
|
||||
dsps_fft2r_fc32(fft_buf, SAMPLE_COUNT >> 1); // operating on half length but complex
|
||||
dsps_bit_rev2r_fc32(fft_buf, SAMPLE_COUNT >> 1); // operating on half length but complex
|
||||
dsps_cplx2real_fc32(fft_buf, SAMPLE_COUNT >> 1); // operating on half length but complex
|
||||
|
||||
for (int i = 0; i < SAMPLE_COUNT / 2; i++)
|
||||
{
|
||||
fft_buf[i] = sqrtf(fft_buf[i * 2 + 0] * fft_buf[i * 2 + 0] + fft_buf[i * 2 + 1] * fft_buf[i * 2 + 1]);
|
||||
}
|
||||
// for (int i = 0; i < SAMPLE_COUNT / 2; i++)
|
||||
// {
|
||||
// fft_buf[i] = 10 * log10f((fft_buf[i * 2 + 0] * fft_buf[i * 2 + 0] +
|
||||
|
@ -210,44 +230,24 @@ static void app_process_task(void *args)
|
|||
|
||||
xSemaphoreTake(output_mutex, portMAX_DELAY);
|
||||
|
||||
// current_powers[band] = 0;
|
||||
// for (int band=START;band<END;band++){
|
||||
// current_powers[band] = ;
|
||||
// }
|
||||
SUM_BAND(0, fft_buf, 1, 4);
|
||||
SUM_BAND(1, fft_buf, 5, 45);
|
||||
SUM_BAND(2, fft_buf, 46, 255);
|
||||
// SUM_BAND(0, fft_buf, 1, 4);
|
||||
// SUM_BAND(1, fft_buf, 5, 42);
|
||||
// SUM_BAND(2, fft_buf, 43, 511);
|
||||
|
||||
// for (int band = 0; band < BANDS_COUNT; band++)
|
||||
// {
|
||||
// current_powers[band] = 0;
|
||||
// for (int freq = 0; freq < FREQS_PER_BAND; freq++)
|
||||
// {
|
||||
// // current_powers[band] += abs(buf[band * FREQS_PER_BAND + freq]);
|
||||
// SAMPLE_TYPE v = buf[band * FREQS_PER_BAND + freq];
|
||||
// if (v < 0)
|
||||
// {
|
||||
// current_powers[band] -= v;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// current_powers[band] += v;
|
||||
// }
|
||||
// if(band==0 && freq<BANDS_COUNT){
|
||||
// avg_powers[freq] = v; //current_powers[band];
|
||||
// }
|
||||
// }
|
||||
|
||||
// uint32_t old_band_avg_power = avg_powers[band];
|
||||
|
||||
// // avg_powers[band] = 0; // (old_band_avg_power >> 1) + (old_band_avg_power >> 2) + (current_powers[band]
|
||||
// >> 2);
|
||||
// }
|
||||
for (int band = 0; band < BANDS_COUNT; band++)
|
||||
{
|
||||
float old_band_avg_power = avg_powers[band];
|
||||
avg_powers[band] = FALLOFF_F(old_band_avg_power, current_powers[band]);
|
||||
}
|
||||
|
||||
xSemaphoreGive(output_mutex);
|
||||
|
||||
xTaskNotify(output_task, next_fft_buf_idx, eSetValueWithOverwrite);
|
||||
|
||||
next_fft_buf_idx = (next_fft_buf_idx + 1) & (BUF_COUNT - 1);
|
||||
|
||||
// printf("[0] %08x [1] %08x [2] %08x [3]%08x ...\n", (buf[0]), (buf[1]), (buf[2]), (buf[3]));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
# ]
|
||||
# ///
|
||||
|
||||
from struct import unpack
|
||||
from struct import Struct
|
||||
from threading import Thread
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
|
@ -16,8 +16,10 @@ from matplotlib.animation import FuncAnimation
|
|||
from serial import Serial
|
||||
|
||||
SYNC = bytes.fromhex("001180007fff1100")
|
||||
BLOCK_LENGTH = 512
|
||||
BLOCK_SIZE = 4 * BLOCK_LENGTH
|
||||
BLOCK_LENGTH = 256
|
||||
BANDS_COUNT = 3
|
||||
|
||||
BlockStruct = Struct(f"I{BANDS_COUNT}f{BANDS_COUNT}f{BLOCK_LENGTH}f")
|
||||
|
||||
data: list[bytes] = []
|
||||
|
||||
|
@ -47,22 +49,32 @@ def recv_main():
|
|||
|
||||
while do_recv:
|
||||
sync()
|
||||
data.append(ser.read(4 + BLOCK_SIZE))
|
||||
data.append(ser.read(BlockStruct.size))
|
||||
|
||||
|
||||
x1 = np.arange(BLOCK_LENGTH)
|
||||
y1 = np.zeros(BLOCK_LENGTH)
|
||||
x2 = np.arange(BLOCK_LENGTH)
|
||||
y2 = np.zeros(BLOCK_LENGTH)
|
||||
x3 = np.arange(BANDS_COUNT)
|
||||
y3 = np.zeros(BANDS_COUNT)
|
||||
x4 = np.arange(BANDS_COUNT)
|
||||
y4 = np.zeros(BANDS_COUNT)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
global recv_thread
|
||||
x1 = np.arange(BLOCK_LENGTH)
|
||||
y1 = np.zeros(BLOCK_LENGTH)
|
||||
x2 = np.arange(BLOCK_LENGTH)
|
||||
y2 = np.zeros(BLOCK_LENGTH)
|
||||
|
||||
fig, ax = plt.subplots()
|
||||
fig, ax = plt.subplots(3)
|
||||
fig.canvas.mpl_connect("close_event", on_close)
|
||||
graph1 = ax.plot(x1, y1)[0]
|
||||
graph2 = ax.plot(x2, y2)[0]
|
||||
plt.ylim(-100, 100)
|
||||
# ax.set_ylim(1 - 2**32, 2**32)
|
||||
graph1 = ax[0].plot(x1, y1, label="fft")[0]
|
||||
graph2 = ax[1].plot(x2, y2, label="floating max")[0]
|
||||
graph3 = ax[2].bar(x3, y3, label="avg")
|
||||
graph4 = ax[2].bar(x4, y4, label="cur")
|
||||
|
||||
ax[0].set_ylim(0, 100)
|
||||
ax[1].set_ylim(-2, 2)
|
||||
ax[2].set_ylim(0, 100)
|
||||
|
||||
np.set_printoptions(suppress=True, precision=2)
|
||||
|
||||
|
@ -71,6 +83,7 @@ def main() -> None:
|
|||
recv_thread.start()
|
||||
|
||||
def update(frame):
|
||||
global x1, x2, x3, x4, y1, y2, y3, y4
|
||||
|
||||
if data:
|
||||
block = data.pop(0)
|
||||
|
@ -81,28 +94,54 @@ def main() -> None:
|
|||
data.clear()
|
||||
|
||||
# print(block.hex())
|
||||
values = unpack(f"I{BLOCK_LENGTH}f", block)
|
||||
values = BlockStruct.unpack(block)
|
||||
|
||||
floating_max = values[0]
|
||||
y1[:] = values[1 : BLOCK_LENGTH + 1]
|
||||
i = 0
|
||||
floating_max = values[i]
|
||||
i += 1
|
||||
avg_powers = values[i : i + BANDS_COUNT]
|
||||
i += BANDS_COUNT
|
||||
cur_powers = values[i : i + BANDS_COUNT]
|
||||
i += BANDS_COUNT
|
||||
|
||||
y1[:] = values[i : i + BLOCK_LENGTH]
|
||||
|
||||
print(f"floating_max:{floating_max}")
|
||||
print(f"avg_pow:{avg_powers}")
|
||||
print(f"cur_pow:{cur_powers}")
|
||||
print(f"min:{np.min(y1)}")
|
||||
print(f"max:{np.max(y1)}")
|
||||
print(f"average:{np.average(y1)}")
|
||||
print(f"median:{np.median(y1)}")
|
||||
|
||||
y2[:] = np.roll(y2,-1)
|
||||
y2[-1] = np.log10((60*floating_max / (2**31)))
|
||||
# print(y2)
|
||||
y2[:] = np.roll(y2, -1)
|
||||
y2[-1] = np.log10((60 * floating_max / (2**31)))
|
||||
|
||||
y3[:] = avg_powers
|
||||
y4[:] = cur_powers
|
||||
|
||||
y1 += 100
|
||||
y3 += 100
|
||||
y4 += 100
|
||||
|
||||
graph1.set_ydata(y1)
|
||||
graph2.set_ydata(y2)
|
||||
for i, b in enumerate(graph3):
|
||||
b.set_height(y3[i])
|
||||
for i, b in enumerate(graph4):
|
||||
b.set_height(y4[i])
|
||||
# graph3.set_height(y3)
|
||||
# graph4.set_height(y4)
|
||||
|
||||
return [graph1]
|
||||
return graph1, graph2, graph3, graph4
|
||||
|
||||
anim = FuncAnimation(
|
||||
fig, update, frames=None, cache_frame_data=False, interval=64 / 24000
|
||||
fig,
|
||||
update,
|
||||
# blit=True,
|
||||
frames=None,
|
||||
cache_frame_data=False,
|
||||
interval=64 / 24000,
|
||||
)
|
||||
plt.show()
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue