instrucmentnote好乱啊,待整理
instrumentnot
1 clear all;clc;close all;
2 load noteoctive
3 %=======================================================
4 strFileName={'violin'};%;flute;;piano1 ;trumpet piano2; trumpet0.5
5 mkdir('parentFolder',strFileName{1});
6 currPath = fileparts(mfilename('fullpath'));%[ST,I] = dbstack('-completenames')
7 limitamp=0.5; %violin0.5;flute0.5;piano0.6 piano1 0.4 piano2 1.5
8 [s1,fs,nbits]=wavread(strFileName{1}); % s1=s1(440000:820000);
9 %=======================================================
10 initialn=25;
11 findthreshold1;
12 finddiv_s;
13 savewavefiles;
findthreshold1
1 B=[1,-0.9375];s=filter(B,1,s1); %clear s1;
2 s=s/max(abs(s)); lenwinms=10;
3 lenwin=(lenwinms/1000)*fs;
4 noverlap=lenwin/3;nfft=lenwin; turesiglength=lenwin-noverlap; %
5 sigFramed = buffer(s,lenwin, noverlap,'nodelay'); %每一列代表一帧
6 %clear s;
7 %amp = sum(abs(sigFramed),1) ;%计算出信号的能量,此处用平均幅度代替
finddiv_s
1 amp(find(amp<limitamp))=0;
2 amplogical=(amp~=0);
3 notei=amplogical;
4 difnotei=diff(notei);
5 Index1=find(difnotei==1);Index2=find(difnotei==-1);
6 lengthI=min(length(Index1),length(Index2));
7 Index1=Index1(1:lengthI);Index2=Index2(1:lengthI);
8 difff=Index2-Index1;
9 difIndex1=find(difff>100);%100difIndex2=find(difff<20);%win=hamming(lenwin);
10 note_num=length(difIndex1);
11 %figure;subrow=ceil(note_num/5);
12 %mkdir('parentFolder1',strFileName{1});
13 for i=1:note_num
14 endpoint1(i)=turesiglength.*(Index1(difIndex1(i))-1)+1;
15 endpoint2(i)=turesiglength.*(Index2(difIndex1(i)));
16 % note{i}=s((turesiglength.*(Index1(difIndex1(i))-1)+1):turesiglength.*(Index2(difIndex1(i))));%%%%????????notei的值才是帧的下标???????
17 note{i}=s(endpoint1(i):endpoint2(i));
18 % % tmp=[note{i}(1);note{i}(1:end-1)];%%zc
19 % % signs=(tmp.*note{i})<0;
20 % % signs=buffer(signs,lenwin, noverlap,'nodelay');
21 % % diffs=abs(tmp-note{i})>25*1e-4;%25*1e-4;3*1e-3; 求过零率时设定了一定的门限,此处为
22 % % diffs=buffer(diffs,lenwin, noverlap,'nodelay');
23 % % zcr=sum(signs.*diffs,1);III=find(zcr~=0,1,'first');JJJ=find(zcr~=0,1,'last');
24 % % onset1(i)=turesiglength.*(III-1)+1;onset2(i)=turesiglength.*(JJJ);
25 % % note{i}=note{i}(onset1(i):onset2(i));
26 % note{i}=note{i}((turesiglength.*(III-1)+1):turesiglength.*(JJJ));
27 % % %
28 notecor=xcorr(note{i});
29 notecor=notecor(round(end/2):end);%notecor=notecor(length(notecor)/2:end);
30 d = diff(notecor);d1=[d(1);d(1:end-1)];
31 indmax = find(d1.*d<0 & d1>0);%%%局部极大值
32 [kk,II]=max(notecor(indmax));
33 pitch(i)=fs./indmax(II);
34
35 dif_pitch=abs(pitch(i)-noteoctive.not_octij);
36 [notelabel_i(i),octivelabel_i(i)]=find(dif_pitch==min(min(dif_pitch)));
37 if i>1&(notelabel_i(i)==notelabel_i(i-1))&(octivelabel_i(i)==octivelabel_i(i-1))
38 wavename{i}=[strFileName{1},'_',noteoctive.notelabel{notelabel_i(i)},noteoctive.octlabel{octivelabel_i(i)},'(2)'];
39 else
40 wavename{i}=[strFileName{1},'_',noteoctive.notelabel{notelabel_i(i)},noteoctive.octlabel{octivelabel_i(i)}];
41 end
42 % % N = sprintf('%6d', [strFileName{1},'_',noteoctive.notelabel{notelabel_i},noteoctive.octlabel{octivelabel_i}])
43 % % if i>1&(wavename{i}==wavename{i-1})
44 % % wavename{i}=strcat(wavename{i},'(2)');
45 % % end
46 % if i<21
47 % subplot(4,5,i);plot(note{i});%%%画出分割结果
48 % % wavename=[strFileName{1},'_',noteoctive.notelabel{notelabel_i},noteoctive.octlabel{octivelabel_i}];
49 % title(wavename{i});
50 % else
51 % if i==21
52 % figure
53 % end
54 % subplot(subrow-4,5,i-20);plot(note{i});%%%画出分割结果
55 % %wavename=[strFileName{1},'_',noteoctive.notelabel{notelabel_i},noteoctive.octlabel{octivelabel_i}];
56 % title(wavename{i});
57 % end
58 % wavwrite(note{i},fs,nbits,[currPath,'\parentFolder\',strFileName{1},'\',wavename{i},'.wav']); %
59 end
60
61 % figure;
62 % plot(s1);hold on;
63 % for i=1:note_num
64 % line([endpoint1(i) endpoint1(i)], [-1 1], 'Color', 'r');hold on;%endpoint1(i)
65 % line([endpoint2(i) endpoint2(i)], [-1 1], 'Color', 'b');hold on;%endpoint2(i)
66 % end
67 % title([strFileName{1},'result']);
68 % hold off;
savewavefiles
1 %savewavefiles;
2 save ('noteesemble.mat','s1','fs','nbits','endpoint1',...
3 'endpoint2','wavename','note_num','currPath','strFileName',...
4 'lenwin','noverlap','turesiglength');%'onset1','onset2',
5 clear all;%clc;
6 load noteesemble.mat;
7 figure;subrow=ceil(note_num/5);
8 for i=1:note_num
9 note{i}=s1(endpoint1(i):endpoint2(i));
10 removenoise;
11 %note{i}=note{i}(onset1(i):onset2(i));
12 if i<21
13 subplot(4,5,i);plot(note{i});
14 % wavename=[strFileName{1},'_',noteoctive.notelabel{notelabel_i},noteoctive.octlabel{octivelabel_i}];
15 title(wavename{i});
16 else
17 if i==21
18 figure
19 end
20 subplot(subrow-4,5,i-20);plot(note{i});
21 %wavename=[strFileName{1},'_',noteoctive.notelabel{notelabel_i},noteoctive.octlabel{octivelabel_i}];
22 title(wavename{i});
23 end
24 wavwrite(note{i},fs,nbits,[currPath,'\parentFolder\',strFileName{1},'\',wavename{i},'.wav']); %
25 end
26 figure;
27 plot(s1);hold on;
28 for i=1:note_num
29 line([endpoint1(i) endpoint1(i)], [-1 1], 'Color', 'r');hold on;%endpoint1(i)
30 line([endpoint2(i) endpoint2(i)], [-1 1], 'Color', 'b');hold on;%endpoint2(i)
31 end
32 title([strFileName{1},' segment result']);
33 hold off;
可去掉removenoise,没用,
removenoise
1 %note{i}\
2 absnote{i}=note{i}/max(abs(note{i})); %
3 sigFramednote = buffer(note{i},lenwin, noverlap,'nodelay');
4 amp = sum(abs(sigFramednote),1) ;
5 noteind=find(amp<(1/30*max(amp)));
6 difnoteind=diff(noteind);
7 maxdifnoteind=max(difnoteind);
8 if maxdifnoteind>100
9 onset=find(difnoteind==maxdifnoteind);
10 note{i}=note{i}((turesiglength.*(noteind(onset)-1)+1):turesiglength.*(noteind(onset+1)));
11 else
12 note{i}=note{i};
13 end


浙公网安备 33010602011771号