How to code this? Resampling of vector

3 vues (au cours des 30 derniers jours)
Lucas
Lucas le 5 Fév 2014
Commenté : Lucas le 5 Fév 2014
Hi all,
Currently I am trying to resample a signal by changing its signal frequency; The signal is given by time vector t and signal vector s. Example:
t=[ 1 2 3 4 5 6 7 8 9 10 11 12]
s=[3 3.5 3 5.5 2 2.5 3 5 7 2.5 3.5 4 ]
I want to create a vector s_new in which the sum of the values of s on the interval <0,4] is one element, the sum of interval <4,8] is an element etc, creating:
s_new= [15 12.5 17]
I have been toying around with histc but didnt succeed. Any ideas on coding this?
Thanks in advance! Lucas
  1 commentaire
Azzi Abdelmalek
Azzi Abdelmalek le 5 Fév 2014
Lucas commented
Thanks guys, but I actually want to use any resampling frequency, so the reshape command doesnt necessarily work (works only if old and new sampling times are integer multiples of each other). Thanks anyway!

Connectez-vous pour commenter.

Réponse acceptée

Mischa Kim
Mischa Kim le 5 Fév 2014
Modifié(e) : Mischa Kim le 5 Fév 2014
For these straightforward time values, this will do
sum(reshape(s,4,3))
If t gets more complex you should be able to find a similar solution, though.

Plus de réponses (2)

David Sanchez
David Sanchez le 5 Fév 2014
s_new(1) = sum(s(find(s<4)))
s_new(2) = sum(s(find( (s<8)&(s>=4) )))
s_new(3) =sum(s(find( (s<12)&(s>=8) )))
...

Azzi Abdelmalek
Azzi Abdelmalek le 5 Fév 2014
Modifié(e) : Azzi Abdelmalek le 5 Fév 2014
t=[ 1 2.5 2.8 3 3.1 4 5 6 7 8 9 10 11 12];
s=[3 3.5 3.5 4 3 5.5 2 2.5 3 5 7 2.5 3.5 4 ]
ii=hist(t,[2 6 10]) % 2 is the middle of the interval [0,4];
ii2=cumsum(ii);
ii1=[1 ii2(1:end-1)+1];
out=arrayfun(@(ii,jj) sum(s(ii:jj)),ii1,ii2)
  1 commentaire
Lucas
Lucas le 5 Fév 2014
Thank you! Works great.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by