Splitting a signal in MATLAB
Printed From: ProjectsQA
Category: Projects Forum
Forum Name: MATLAB Projects
Forum Discription: Projects and discussion on various areas of Matlab like Computer Vision, Image Processing, Signal Processing, Wireless Communication, Deep Learning, Data Analytics, Robotics, Control Systems
URL: http://projectsqa.com/forum_posts.asp?TID=2489
Printed Date: 25Apr2018 at 8:33pm
Topic: Splitting a signal in MATLAB
Posted By: Krishna_Sridhar
Subject: Splitting a signal in MATLAB
Date Posted: 06Feb2018 at 6:29pm
I have a signal that is 40 seconds long. I need to split it into 5 equal sections of 8 seconds.
I could do this the long way by:
let my signal = sig
then
subsig1 = sig(1:8*Fs)
subsig2 = sig((8*Fs)+1:16*Fs)
subsig3 = sig((16*Fs)+1:24*Fs)
subsig4 = sig((24*Fs)+1:32*Fs)
subsig5 = sig((32*Fs)+1:40*Fs)
However, I want to do this in a loop. Can anyone help?

Replies:
Posted By: siva
Date Posted: 06Feb2018 at 6:30pm
There is nothing wrong with what you did, but if you want to do it in a loop, you could put the signal into a 2D array, rather than separate 1D vectors.
Code ( (Unknown Language)):
for k=1:5
subsig(k,:)=sig( 1+(k1)*8*Fs : k*8*Fs )
end
Then if you need separate variables you can do what you did in a straightforward way.
Code ( (Unknown Language)):
1. subsig1=subsig(1,:)
2. subsig2=subsig(2,:)
3. subsig3=subsig(3,:)
4. subsig4=subsig(4,:)
5. subsig5=subsig(5,:)
There may be other ways to do it. In the old days, using FORTRAN, we could do this with no work at all by using a COMMON block and associating two variable array names to the same data. One array was declared as a 2D array and the other as a 1D array. The variables names basically shared the same locations in memory so that if you changed the contents of the memory using one variable name, it automatically was changed for the other variable name. I don't know that you can do this in Matlab, but it might be worth checking. Otherwise it's just a matter of pointing to the correct locations, but do it in a way that is efficient in Matlab. Running nested loops and grabbing individual values is very slow in Matlab. It's best to use vector operations as you did originally.

Posted By: Krishna_Sridhar
Date Posted: 06Feb2018 at 6:30pm
clear all;
clc;
close all;
% assume the following data
Fs = 100;
sig = 1:40;
count = 0;
for k = 1:8:length(sig);
count = count + 1;
sub_sig = sig(k:k+81).*Fs;
fprintf('\n')
fprintf('Set Number %1.0f \n',count);
fprintf('============\n')
fprintf('%4.0i\n',sub_sig);
end

Posted By: Krishna_Sridhar
Date Posted: 06Feb2018 at 6:32pm
Sorry I just joined the forum
try this code
clear all;
clc;
close all;
% assume the following data
Fs = 100;
sig = 1:40;
count = 0;
for k = 1:8:length(sig);
count = count + 1;
sub_sig = sig(k:k+81).*Fs;
fprintf('\n')
fprintf('Set Number %1.0f \n',count);
fprintf('============\n')
fprintf('%4.0i\n',sub_sig);
end

