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 个评论

Jan
Jan 2017-8-9
编辑:Jan 2017-8-9
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.

请先登录,再进行评论。

回答(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);

产品

回答:

Jan
2017-8-9

Community Treasure Hunt

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

Start Hunting!

Translated by