Algorithm for traversing a vector
So I'm having a simple logic problem but I can't find a good solution.
Problem: I'm wanting to scroll through a log file (one .csv whose which I have to solve a calculation of RMS-Root mean Square). But I want my last loop value to be the first of the next iteration.
Simplified example:
int array[3];
Primeira iteração:
array [0] = 1;
array [1] = 2;
array [2] = 3;
Segunda iteração:
array [0] = 3;
array [1] = 4;
array [2] = 5;
Terceira iteração:
array [0] = 5;
array [1] = 6;
array [2] = 7;
I exemplified above in a simple way for the best understanding of the problem.
The situation real is as follows:
Basically I need to perform an RMS calculation, my window is 480 samples, but I need to superimpose 160 samples to each iteration ( i.e. take the last 160 values and make them the first of the next iteration), so I thought the best way is to separate the window into 3 samples of size 160 (totaling 480 samples), and assign these samples to indices of a control vector:
int controle[3];
Primeira iteração:
controle[0] = dados{0~159};
controle[1] = dados{160~319};
controle[2] = dados{320~479}; // Valor a ser utilizado na sobreposição.
Segunda iteração:
controle[0] = dados{320~479}; // isto é a sobreposição que citei.
controle[1] = dados{480~639};
controle[2] = dados{640~799};// Valor a ser utilizado na sobreposição.
Terceira iteração:
controle[0] = dados{640~799}; // isto é a sobreposição que citei.
controle[1] = dados{800~959};
controle[2] = dados{960~1120};
I am working with audio tracks at the frequency of 16kHz I need 30ms (480 samples) windows with 10ms(160 samples) overlap to perform the RMS calculation.
Follows the attempt to encode this problem, I removed the file manipulation parts to make it more evident:
#include <stdio.h>
int main() {
int j = 0, a = 0, i = 0; // variaveis de iteração.
int inicio = 0; // Inicio do contador de 160
int fim = 160; // Fim do contador de 160
int inicio_principal = 0; // contador do vetor de controle.
int fim_principal = 3; //
// Loop externo para limitar a execução no debug.
while (a < 1) {
// Loop para fazer a contagem de 0 a 2 do vetor de controle.
for(i = inicio_principal; i < fim_principal; i++){
printf("Loops Externos I = %d\n\n",i); // Debug visual
// Loop para carregar o vetor com os dados do csv
for(j = inicio; j < fim; j++){
printf("J = %d\n",j); //Debug visual.
// Aqui preenche o vetor com os 30ms de dados.
}
// Faz os incrementos de 160 em 160;
inicio = fim;
fim = fim + 160;
}
a++; // contador para parar o while.
}
return 0;
}
Any suggestions to better solve this problem?
1 answers
Scroll through samples in "window - overlay" steps:
#include <math.h>
// ...
for (int i = 0; i < total_amostras; i += janela - sobreposicao) {
int meanSquare = 0;
// Percorre a janela (com a sobreposição) calculando a média dos
// quadrados
for (int j = 0; j < janela; j++) {
int amostra = amostras[i + j];
meanSquare += amostra * amostra / janela; // mean square das amostras da janela
}
int rms = sqrt(meanSquare);
// Use seu RMS da janela como quiser aqui
}
// ...
You will need to set some values as well:
-
total_amostras
: total number of samples -
janela
: window size (in your case, 480) -
sobreposicao
: overlay (in your case, 160)
It is worth noting that your total samples have to be multiple of 320.