how to vectorize this for loop

tic; [s,fs]=audioread('sample.wav'); fdatool
l=length(s); figure('name','speechsignal');
subplot(3,3,1); plot (s); title('speech signal'); xlabel('samples---->'); ylabel('amplitudes---->');
t=(0:1:l-1)/fs; subplot(3,3,2); plot(t,s); title('Speech signal on time axis'); xlabel('time(s) ---->'); ylabel('Amplitude ---->');
f_d=0.1; f_s=floor(l*f_d); overlap=f_s/2; no_f=floor((l/f_s)*2-1); n=zeros(f_s,no_f); n = s( bsxfun(@plus, (1:f_s).', ((1:no_f)-1)*overlap ) );
subplot(3,3,3);
plot(n);
title('Framing');
xlabel('frames');
ylabel('amplitude');
time=toc
% %windowing
w=hamming(f_s);
for i=1:no_f;
xw(:,i)=w.*n(:,i);
end

1 Comment

Jan
Jan on 9 Aug 2017
Edited: Jan on 9 Aug 2017
Please mark your code with the mouse and hit the "{} Code" button. Afterwards your code is readable, which is a great benefit for the readers.
Please post the relevant part of the code only.

Sign in to comment.

Answers (1)

I assume you can replace the loop
for i=1:no_f;
xw(:,i)=w.*n(:,i);
end
by:
xw = w .* n; % >= R2016b
For older Matlab versions:
xw = bsxfun(@times, w, n);

Products

Asked:

on 9 Aug 2017

Answered:

Jan
on 9 Aug 2017

Community Treasure Hunt

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

Start Hunting!