Info

This question is closed. Reopen it to edit or answer.

Please help me with my loop. I can´t get my loop to work, i am not so skilled in MatLab and I have no problem with getting my results but with this loop working my code will be much shorter and easier to read for my supervisor.

1 view (last 30 days)
%========================break down and rebuild============================
%===========Destroy==============Erase==============Improve================
close all;clear all;clc;
[X,fsx] = wavread('XXX');
[Y,fsy] = wavread('YYY');
X = resample(X,44100,fsx);
Y = resample(Y,44100,10976);
%==========================================================================
load cal_high_new
t = 1/fs;l = length(acq_sig);T = t*l; %Creating time vector
l_low = 5/t;l_h = 22/t;l_high = 10/t; %Choosing start and stop time
x_1 = acq_sig(2,:);x_1 = x_1(l_low:l_high); %Cutting the chosen part from
%signal
x_1 = x_1./sqrt(2);
x_2 = acq_sig(1,:);x_2 = x_2(l_low:l_high); %Cutting correct time from smpl
p_ref = 2*10^(-5); %Reference pressure
Lp_meas = 20*log10(x_1./p_ref); %
for i = 1:1:length(x_2);
Z = 10*log10(sum(10^(Lp_meas(i)/10)));
end
c_Z = 10^((94 - Z - 47)/20); %47 is for the amplification i audacity
c_Z = real(c_Z);
%====================tersbandsindela och källstyrkebesämma=================
fs=fsy;
v = 8; %source speed [m/s]
x = 6.85; %distance from mic to path
T = 10;
t_d = -(T/2):(1/fs):(T/2);
t = 0:1:10*fs; %Time vector
%t2 = 0:1:10*fsx;
d = t_d.*v; %source position vector
%==========================================================================
fs = 44100;
N=size(X); %creates an empty vector in size of y
N_use=2^10*floor(N/2^10); %Rounding of to the closest N used
y_bnd=filter_noise_to_thirds(fs,X(1:N_use)); %filter the signal into 1/3:s
L = length(y_bnd(:,1)); %Same length for all
nfft = 2^nextpow2(L); % Next power of 2 from length of y
%==============================
for i = 1:30;
Z(i) = fft(y_bnd(:,i),nfft)./L;
end
f = fs/2*linspace(0,1,nfft/2+1);
% for j = 1:30;
% Q(i) = 2*abs(Z(j:nfft/2+1));
% end
% for k=1:30;
% Q_SPL(k) = 10*log10((c_Z*Q(k).^2)./p_ref.^2)-6;
% end
% for l=1:30;
% Q_Lw(l) = Q_SPL(l) - 10*log10(1/(4*pi*x^2));
% end
% for m=1:30;
% Lw_1(m) = max(Q_Lw(m));
% end
%==============================

Answers (2)

Star Strider
Star Strider on 6 Aug 2014
Edited: Star Strider on 6 Aug 2014
I don’t understand what you are doing with this loop:
for i = 1:1:length(x_2);
Z = 10*log10(sum(10^(Lp_meas(i)/10)));
end
but it is not going to save the individual Z values.
You need to store the ffts in a matrix, rather than a vector, and preallocate Z:
Z = zeros(L,30);
for i = 1:30;
Z(:,i) = fft(y_bnd(:,i),nfft)./L;
end
  2 Comments
Björn Olsson
Björn Olsson on 7 Aug 2014
Thank you for the help, I am very grateful. There are probably parts that not should be in the final code here and there, it is also not the full code for the program. Again thanks for your help. Cheers Björn
Star Strider
Star Strider on 7 Aug 2014
My pleasure!
The problem with creating Z in the first loop is that it creates a variable or vector that is going to conflict with your using it in the second loop as a matrix. I suggest you rename the Z in the first loop (and wherever it is later used) something slightly different so you don’t get that conflict.

Chad Greene
Chad Greene on 7 Aug 2014
For readability, spl calculations may be performed with this function.

Tags

Community Treasure Hunt

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

Start Hunting!