I think there is a little mistake in the sampledValues you are expecting: times = [0, 2, 6, 8, 14] values= [5, 9, 10, 1, 6] sampledTime = [0, 2, 4, 6, 8, 10, 12, 14] sampledValues = [5, 9, 9, 10, 1, 1, 1, 6] According to the rule used for the previous values, the last value of sampledValues must be 1 and not 6
Here is a code doing what you expect (but with a different last point) times = [0, 2, 6, 8, 14]; values= [5, 9, 10, 1, 6]; sampletimes=0:14; V=zeros(sampletimes); for k=1:size(times,'*')-1 V(sampletimes>=times(k)&sampletimes<times(k+1))=values(k); end V(sampletimes>times($))=timesvalues($); clf;plot(sampletimes,V,'-+') //You can also use linear interpolation V1=interpln([times;values],sampletimes); plot(sampletimes,V1,'r-+') //or Splin interpolation V2=interp(sampletimes,times,values,splin(times,values)) plot(sampletimes,V2,'m-*') Serge Steer ----- Mail original ----- > De: "Matiasb" <[email protected]> > À: [email protected] > Envoyé: Mardi 18 Février 2014 20:59:10 > Objet: [Scilab-users] How to get a reguar sampled vector in Scilab > > I'm trying to program function (or even better it it already exists) in > scilab that calculates a regular timed samples of values. IE: I have a > vector 'values' which defines contains the value of a signal at different > times. This times are in the vector 'times'. So at time times(N), the signal > has value values(N). At the moment the times are not regular, so the > variable 'times' and 'values' can look like: > > times = [0, 2, 6, 8, 14] > values= [5, 9, 10, 1, 6] > This represents that the signal had value 5 from second 0 to second 2. Value > 9 from second 2 to second 6, etc. Therefore, if I want to calculate the > signal average value I can not just calculate the average of vector > 'values'. This is because for example the signal can be for a long time with > the same value, but there will be only one value in the vector. One option > is to take the deltaT to calculate the media, but I will also need to > perform other calculations:average, etc. > > Other option is to create a function that given a deltaT, samples the time > and values vectors to produce an equally spaced timeVector and corresponding > values. For example, with deltaT=2 and the previous vectors, > > [sampledTime, sampledValues] = regularSample(times, values, 2) > sampledTime = [0, 2, 4, 6, 8, 10, 12, 14] > sampledValues = [5, 9, 9, 10, 1, 1, 1, 6] > > This is easy if deltaT is small enough to fit exactly with all the times. If > the deltaT is bigger, then the average of values or some aproximation must > be done... > > Is there anything already done in Scilab? How can this function be > programmed? > > Thanks a lot! PS: I don't know if this is the correct forum to post scilab > questions, so any pointer would also be useful. > > > > -- > View this message in context: > http://mailinglists.scilab.org/How-to-get-a-reguar-sampled-vector-in-Scilab-tp4028712.html > Sent from the Scilab users - Mailing Lists Archives mailing list archive at > Nabble.com. > _______________________________________________ > users mailing list > [email protected] > http://lists.scilab.org/mailman/listinfo/users > _______________________________________________ users mailing list [email protected] http://lists.scilab.org/mailman/listinfo/users
