noteMatch.m
资源名称:10_11.rar [点击查看]
上传用户:abcwxk
上传日期:2016-05-20
资源大小:434k
文件大小:2k
源码类别:
midi
开发平台:
Visual C++
- function [names_top5, costs_top5] = noteMatch(notes_steps);
- L2 = length(notes_steps);
- % 读取库
- load midLib; % 库中一首歌的midi音符序列
- count = midLib{1,1};
- midData = midLib{1,2};
- costs_top5 = [NaN NaN NaN NaN NaN;...
- 0 0 0 0 0];
- names_top5 = cell(1,5);
- % 遍历,寻找top10最低差错的序列,返回
- for n = 1:count
- % 读取第n个歌曲的相对音高序列
- notes_relative = midData{n,2};
- % 在歌曲中寻找最小距离的一段
- % 2字符串的相似矩阵 M,相同表示0,不同表示1
- % 例如:1 2 -2 4 -3 0
- L1 = length(notes_relative);
- M = zeros(L1, L2);
- for i = 1:L1
- for j=1:L2
- M(i,j) = (notes_relative(i) ~= notes_steps(j));
- end
- end
- % 寻找最相似的片段起始位置,并计算错误字符数
- sim = zeros(1,(L1 - L2 +1));
- for i = L2: L1
- tmp = 0;
- for j = 1: L2
- tmp = tmp + M( j+i-L2, j);
- end
- sim(i - L2 + 1) = tmp;
- end
- [cost,N] = min(sim);
- %
- disp(['cost: ' num2str(cost) ' 开始音符位置:' num2str(N) ]);
- % 更新costs_top5
- if sum( isnan(costs_top5(1,:)) ) ~= 0 % 包含NaN
- tmp1 =find( isnan(costs_top5(1,:)) == 1);
- costs_top5(1, tmp1(1)) = cost;
- costs_top5(2, tmp1(1)) = n;
- else
- [tmp1,tmp2] = max(costs_top5(1,:)); % 找到costs中最大的一个,更新这个
- if cost < tmp1;
- costs_top5(1, tmp2) = cost;
- costs_top5(2, tmp2) = n;
- end
- end
- end
- for i = 1:5
- names_top5(i) = midData(costs_top5(2,i),1);
- end
- costs_top5 = costs_top5(1,:);
- return;