# How do I design a for-loop to sample 3 seconds of a signal every 15 seconds?

1 view (last 30 days)
Sebastian on 13 May 2015
Answered: Purushottama Rao on 13 May 2015
Hey, I'm working on some signal processing in matlab but I'm still quite new to it.
I wish to sample 3 seconds of a signal, every 15 seconds, for the length of the signal but so far I've only successfully hardcoded it. If someone can help me make a for-loop to do this instead I would greatly appreciate it as it would make the code more versatile to different signal-lengths.
Snippet of the hardcoded bit:
x=signal(1:Fs*3+1);
y=sqrt(sum(x.^2)/length(x));
x=signal(Fs*14+1:Fs*17+1);
y=[y; sqrt(sum(x.^2)/length(x))];
x=signal(Fs*29+1:Fs*32+1);
y=[y; sqrt(sum(x.^2)/length(x))];
with Fs being my samplerate.
Essentially each step just grabs some data points from the signal and appends the RMS value to 'y'
The first step of "Fs*X+1" grabs the first 3 seconds of data. Every subsequent step grabs data from 1s before the factors of 15 to 2s after. so 14s-17s, 29s-32s, 44s-47s, 59s-62s and so on.
I hope that makes a bit of sense because to me it looks like it should be quite easy to sum up in a for-loop. I just can't figure it out on my own it seems...

Purushottama Rao on 13 May 2015
f= rand(6);
f=f(:);
k=1;
for i=1:1:length(f)
if (mod(i,15)<3)
b(k)=f(i);
k=k+1;
end
end
b=[1 b];
b(1:3)=f(1:3);
disp('b=')
disp(b)
if true
% code
end
Try this

### Categories

Find more on Spectral Measurements in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!