Error "undefined function or variable 'y1' ". Help me please. :\

if true
% code
end--------------------------------start of the code
clc;
clear all;
Fs=8000;
for k=1:30
clear y1 y2 y3;
display('record voice');
pause();
x=wavrecord(Fs,Fs);
t=0.04;
j=1;
for i=1:8000
if(abs(x(i))>t)
y1(j)=x(i);
j=j+1;
end
end
y2=y1/(max(abs(y1)));
y3=[y2,zeros(1,3120-length(y2))];
y=filter([1 -0.9],1,y3');%high pass filter to boost the high frequency components
%%frame blocking
blocklen=240;%30ms block
overlap=80;
block(1,:)=y(1:240);
for i=1:18
block(i+1,:)=y(i*160:(i*160+blocklen-1));
end
w=hamming(blocklen);
for i=1:19
a=xcorr((block(i,:).*w'),12);%finding auto correlation from lag -12 to 12
for j=1:12
auto(j,:)=fliplr(a(j+1:j+12));%forming autocorrelation matrix from lag 0 to 11
end
z=fliplr(a(1:12));%forming a column matrix of autocorrelations for lags 1 to 12
alpha=pinv(auto)*z';
lpc(:,i)=alpha;
end
wavplay(x,Fs);
X(k,:)=reshape(lpc,1,228);
Y(k,:)=input('enter the number ');
end
save('lpcdata.mat','X','Y');
---------------------------------------the command
record voice
??? Undefined function or variable 'y1'.
Error in ==> normalizedlpc at 17
y2=y1/(max(abs(y1)));

 Respuesta aceptada

This
j=1;
for i=1:8000
if(abs(x(i))>t)
y1(j)=x(i);
j=j+1;
end
end
can be vectorized:
y1 = zeros(1, length(x); % Initialize to all zeros.
% Compute logical indexes of where x > t
aboveThreshold = x > t;
if sum(aboveThreshold) > 1
% At least one element of x is above the threshold.
% Extract only those above threshold and create a new y1
% which may be a different length than the initialized one.
y1 = x(aboveThreshold);
end
but you still might want to find out why no x is above your t threshold, because though my code fixed your code, it will just continue on with a y of all zeros, which is probably useless and not what you want.

7 comentarios

Poison, did our help ever resolve the problem you were/are having?
Poison
Poison el 20 de Oct. de 2014
I don't understand your vectorized code. sorry. I tried remove the if(abs(x(i))>t), the output is wrong. It's not accurate as before i formatted my pc.
-this code works fine before i formatted my pc tho.
Tbh i'm a total newbie on matlab. my bad, forgot to clear it first hand.
And this code i found it on some site. tho i will need to edit it as my input and no of input is different. But I can't do it if I can't even run this code.
Why did you format the PC? Was your hard drive having problems reading files?
You can use your non-vectorized way if you want. But tell me what this says if you put it before the loop
maxVal = max(x)
If maxVal is less than t, you are not selecting a good threshold because your signal is never louder than that.
Nah, OS problem. But nvm.
Thanks for your explanation, now I know about the threshold and the problem solved.
Do you mind explain about the xcorr/ correlation in the code. I don't understand why it is finding auto correlation from lag -12 to 12. Is it because of the 10 samples(input 10x3=30)?
for i=1:19
a=xcorr((block(i,:).*w'),12); %finding auto correlation from lag -12 to 12
for j=1:12
auto(j,:)=fliplr(a(j+1"j+12)); %forming autocorrelation matrix from lag 0 to 11
end
z=fliplr(a(1:12)); %forming a column matrix of autocorrelations for lags 1 to 12
alpha=pinv(auto)*z';
lpc(:,i)=alpha;
end
The correlation is like you slide and multiply two signals (or the same signal) and sum the result. Normally you slide them the entire way so that the first element is where the right element of the signal on the left overlaps the left element of the signal on the right. They overlap at just that one element. The lag would be the full signal length. Then you slide so that they overlap by two elements. If you want, there is an option to limit the amount of distance they're allowed to be apart (how far away their centers are when they overlap). For example you might know in advance that once the lag (separation) is more than 12 elements, the autocorrelation is basically flat. If the full correlation shows a peak around 0 and after a shift of around 4 or 6 the correlation is essentially zero or a constant, then why go all the way out to the far end? Let's say the signal is 1000 elements long. Why go all the way out to a shift of 1000 elements when all the action is taking place in a shift of less than 12? It will just take longer and not provide any useful information. The 12 is a parameter you can choose based on your experience with that signal. Do the full correlation for some signals and see where all the action takes place and pick your maxLag accordingly.
Poison
Poison el 28 de Oct. de 2014
Thank you for the explanation. Meant alot and 1 more question.
Why is my .mat(microsoft access table) files is only a shortcut after i run the program. Is there any way to make in permanently stay in my folder as .mat? And if there isn't, how do i save data into microsoft access table that is open-able afterward?
Thanks in advanced.

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

el 18 de Oct. de 2014

Comentada:

el 28 de Oct. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by