0001 function sampleIndex = getlocation(y, fs, pitch, candNum, PorV, maxPeriod, minPeriod, plotopt)
0002
0003
0004
0005
0006
0007
0008
0009 if nargin <5, plotopt = 0; end;
0010 if nargin <3, maxPeriod = 0.007; minPeriod = 0.0065; plotopt = 1; end;
0011 if nargin==0, selfdemo; return; end;
0012
0013 Xdistscale = 20;
0014 Ydistscale = 0.2;
0015
0016 if PorV == 0,
0017 [junk, pmark] = max(y);
0018 else
0019 [junk, pmark] = min(y);
0020 end;
0021
0022 minYhead = pmark-50; if minYhead <= 0, minYhead = 1; end;
0023 minYtail = pmark+50; if minYtail > length(y), minYtail = length(y); end;
0024 sampleIndex = getIndex(y(minYhead:minYtail), minYhead, minYtail, candNum, Xdistscale, Ydistscale, PorV);
0025 singlePeriod = (fs/pitch);
0026
0027
0028 while (1),
0029 currentIndex = sampleIndex(end, 1);
0030 minIndex = currentIndex+floor(minPeriod*fs);
0031 maxIndex = currentIndex+ceil(maxPeriod*fs);
0032 if maxIndex >= length(y) ,
0033 if (length(y)- sampleIndex(end, 1)) > singlePeriod,
0034 begin = sampleIndex(end, 1) + round(singlePeriod/2);
0035 candidateY = y(begin : end);
0036 tempIndex = getIndex(candidateY, begin, length(y), candNum, Xdistscale, Ydistscale, PorV);
0037 sampleIndex = [sampleIndex; tempIndex];
0038 end;
0039 break;
0040 end;
0041 candidateY = y(minIndex:maxIndex);
0042 tempIndex = getIndex(candidateY, minIndex, maxIndex, candNum, Xdistscale, Ydistscale, PorV);
0043 sampleIndex = [sampleIndex; tempIndex];
0044 end;
0045
0046
0047 while (1)
0048 currentIndex = sampleIndex(1, 1);
0049 maxIndex = currentIndex-floor(minPeriod*fs);
0050 minIndex = currentIndex-ceil(maxPeriod*fs);
0051 if minIndex <= 0 ,
0052 if sampleIndex(1, 1) > singlePeriod,
0053 tail = sampleIndex(1, 1) - round(singlePeriod/2);
0054 candidateY = y(1:tail);
0055 tempIndex = getIndex(candidateY, 1, tail, candNum, Xdistscale, Ydistscale, PorV);
0056 sampleIndex = [tempIndex; sampleIndex];
0057 end;
0058 break;
0059 end;
0060 candidateY = y(minIndex:maxIndex);
0061 tempIndex = getIndex(candidateY, minIndex, maxIndex, candNum, Xdistscale, Ydistscale, PorV);
0062 sampleIndex = [tempIndex; sampleIndex];
0063 end;
0064
0065 sampleIndex = sampleIndex';
0066
0067
0068 if plotopt,
0069 figure;
0070 pmark = sampleIndex(1, 1:end-1);
0071 subplot(2,1,1);plot((1:length(y)), y, '.-');
0072
0073 for i=1:length(pmark),
0074 line(pmark(i), y(pmark(i)), 'marker', 'o', 'color', 'r');
0075 end
0076 axis([-inf inf -1 1]);
0077 axis([0.1 0.8 -1 1]);
0078 ylabel('Amplitude');
0079 title('Wave form');
0080 axis tight;
0081 legend('wave','pitch marks');
0082
0083 subplot(2,1,2);
0084 dpmark = diff(pmark);
0085 pitch = fs./dpmark;
0086 plot(pitch, 'ro-');
0087 end;
0088
0089
0090 function tempIndex = getIndex(candidateY, minIndex, maxIndex, candNum, Xdistscale, Ydistscale, PorV)
0091
0092 if PorV == 0,
0093 [value, idx] = localmax(candidateY);
0094 else
0095 [value, idx] = localmin(candidateY);
0096 end;
0097
0098
0099 if isempty(idx),
0100 idx = round((minIndex + maxIndex)/2 - minIndex);
0101 value = candidateY(idx);
0102 end;
0103
0104
0105 dist = abs(value - value(1));
0106 dist = dist/( max(candidateY) - min(candidateY));
0107 idx = idx(find(dist < Ydistscale));
0108 Index = idx(1);
0109 range = [Index - Xdistscale : Index + Xdistscale];
0110 idx = idx(find(ismember(idx, range)));
0111
0112
0113 plen = length(idx);
0114 if plen>candNum, plen = candNum; end;
0115 tempIndex = minIndex+idx(1:plen)-1;
0116 if length(tempIndex)< candNum,
0117 cnts = candNum - length(tempIndex);
0118 tempIndex = [tempIndex tempIndex(end)*ones(1, cnts)];
0119 end;
0120
0121
0122 function selfdemo
0123 wavefile = 'la.wav';
0124 plotopt = 0;
0125
0126 [y,fs] = wavread(wavefile);
0127 y = y-mean(y);
0128
0129 y = y(3900:7300);
0130
0131 [p0, p1, p2, p3, p4] = wave2pitch(y,fs,0);
0132 p2 = p2(find(p2~=0));
0133 pitch = nanmedian(p2);
0134 if isempty(p2) | isempty(pitch),
0135 fprintf('The wavefile has some problems!\n');
0136 return;
0137 end;
0138 pitch = smtn2frq(pitch);
0139 a = (1/pitch);
0140 b = a*0.5;
0141 maxPeriod = a + b;
0142 minPeriod = a - b;
0143 pitch = mean(pitch);
0144 candNum = 3;
0145 PorV = 1;
0146 sampleIndex = feval(mfilename, y, fs, pitch, candNum, PorV, maxPeriod, minPeriod, 1);