how to save frames of data into one struct?

2 visualizaciones (últimos 30 días)
Jun W
Jun W el 13 de Mayo de 2018
Comentada: Walter Roberson el 25 de Mayo de 2018
I have the following code to read one frame data--> n points-->find 10 peaks(from n points). say I want to read 10000 frames of data, then save all the (1000*10)peaks into a struct. how do I modify the code?
a=arduino
n=20;
for ii=1:n
v(ii)=readVoltage(a,'A0');
end
[pks,loc]=findpeaks(real(fft(v)),'npeaks',10);

Respuesta aceptada

Walter Roberson
Walter Roberson el 13 de Mayo de 2018
repeat_count = 10000;
my_data(repeat_count) = struct('pks', [], 'loc', []); %initialize for efficiency
n=20;
for count = 1 : repeat_count
v = zeros(1, n);
for ii=1:n
v(ii) =readVoltage(a,'A0');
end
[my_data(count).pks, my_data(count).loc] = findpeaks(real(fft(v)),'npeaks',10);
end
Note: if you want 10 peaks out of 20 points. then you will need to select index 2 through 11 from the fft result.
Selecting peaks from the entire fft result is not the right thing to do because fft of real data returns complex conjugate data symmetric around the middle of the output.
Because peak selection requires that the point before and the point after be less than or equal to the current point, the maximum number of peaks that can be located is half of the input size -- and if you are going for that many you might as well just take every second point.
Effectively the maximum number of peaks you can find from real(fft(v)) is length(v)/4, and you should probably be aiming for less than that.
  6 comentarios
Jun W
Jun W el 24 de Mayo de 2018
Editada: Walter Roberson el 25 de Mayo de 2018
It worked! Thank you. But when I pass it to the classifier, there's always one row/column unsuitable, is it because it needs one row/column as label?
Also to get the first half of fft, instead of
[pks,loc]=findpeaks(real(fft(v)),'npeaks',10);
should the correct code be:
Y = fft(v);
P2 = abs(Y/length(v));
P1 = P2(1:length(v)/2+1);
[my_data(count).pks, my_data(count).loc] = findpeaks(P1,'npeaks',5);
but what is the length of the signal?
Walter Roberson
Walter Roberson el 25 de Mayo de 2018
? length of the signal would be length(v) like you are using now, so I am not sure what you are asking about?
I suggest, by the way,
P1 = P2(1:ceil(end/2));

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by