📄 dtwplot.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"><html><head> <title>Description of dtwPlot</title> <meta name="keywords" content="dtwPlot"> <meta name="description" content="DTWPLOT Plot the result of DTW of two pitch/MFCC vectors"> <meta http-equiv="Content-Type" content="text/html; charset=big5"> <meta name="generator" content="m2html © 2003 Guillaume Flandin"> <meta name="robots" content="index, follow"> <link type="text/css" rel="stylesheet" href="../m2html.css"></head><body><a name="_top"></a><div><a href="../index.html">Home</a> > <a href="index.html">dcpr</a> > dtwPlot.m</div><!--<table width="100%"><tr><td align="left"><a href="../index.html"><img alt="<" border="0" src="../left.png"> Master index</a></td><td align="right"><a href="index.html">Index for dcpr <img alt=">" border="0" src="../right.png"></a></td></tr></table>--><h1>dtwPlot</h1><h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2><div class="box"><strong>DTWPLOT Plot the result of DTW of two pitch/MFCC vectors</strong></div><h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2><div class="box"><strong>function dtwplot(vec1, vec2, DTWpath, DTWpath2) </strong></div><h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2><div class="fragment"><pre class="comment">DTWPLOT Plot the result of DTW of two pitch/MFCC vectors
Usage:
dtwplot(vec1, vec2, DTWpath)
For example:
vec1=[71 73 75 80 80 80 78 76 75 73 71 71 71 73 75 76 76 68 76 76 75 73 71 70 70 69 68 68 72 74 78 79 80 80 78];
vec2=[69 69 73 75 79 80 79 78 76 73 72 71 70 70 69 69 69 71 73 75 76 76 76 76 76 75 73 71 70 70 71 73 75 80 80 80 78];
[minDist, dtwPath, dtwTable] = dtw1(vec1, vec2);
dtwplot(vec1, vec2, dtwPath);</pre></div><!-- crossreference --><h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>This function calls:<ul style="list-style-image:url(../matlabicon.gif)"></ul>This function is called by:<ul style="list-style-image:url(../matlabicon.gif)"><li><a href="dtw3.html" class="code" title="function [minDist, dtwPath, dtwTable]=dtw3(vec1, vec2, beginCorner, endCorner, plotOpt)">dtw3</a> dtw2: Dynamic time warping with local paths of 0 and 45 degrees, presumably for the comparison of music mid vector to note vector</li><li><a href="dtwFixedPoint.html" class="code" title="function [minDist, allDist, allPitchShift]=dtwFixedPoint(inputPitch, dbPitch, param, plotOpt)">dtwFixedPoint</a> dtwFixedPoint: Use of Picard iteration on finding the optimal pitch shift for DTW</li><li><a href="keyTransposition01.html" class="code" title="">keyTransposition01</a> This script demonstrate the use of Picard iteration on finding the optimal pitch shift for DTW in melody recognition</li></ul><!-- crossreference --><h2><a name="_subfunctions"></a>SUBFUNCTIONS <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2><ul style="list-style-image:url(../matlabicon.gif)"><li><a href="#_sub1" class="code">function selfdemo</a></li></ul><h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2><div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function dtwplot(vec1, vec2, DTWpath, DTWpath2) </a>0002 <span class="comment">%DTWPLOT Plot the result of DTW of two pitch/MFCC vectors</span>0003 <span class="comment">% Usage:</span>0004 <span class="comment">% dtwplot(vec1, vec2, DTWpath)</span>0005 <span class="comment">%</span>0006 <span class="comment">% For example:</span>0007 <span class="comment">% vec1=[71 73 75 80 80 80 78 76 75 73 71 71 71 73 75 76 76 68 76 76 75 73 71 70 70 69 68 68 72 74 78 79 80 80 78];</span>0008 <span class="comment">% vec2=[69 69 73 75 79 80 79 78 76 73 72 71 70 70 69 69 69 71 73 75 76 76 76 76 76 75 73 71 70 70 71 73 75 80 80 80 78];</span>0009 <span class="comment">% [minDist, dtwPath, dtwTable] = dtw1(vec1, vec2);</span>0010 <span class="comment">% dtwplot(vec1, vec2, dtwPath);</span>0011 0012 <span class="comment">% Roger Jang, 20020428, 20070522</span>0013 0014 <span class="keyword">if</span> nargin<1; <a href="#_sub1" class="code" title="subfunction selfdemo">selfdemo</a>; <span class="keyword">return</span>; <span class="keyword">end</span>0015 0016 <span class="keyword">if</span> size(vec1,1)>1 & size(vec1,2)>1 & size(vec2,1)>1 & size(vec2,2)>1 <span class="comment">% Inputs are MFCC matrices for ASR</span>0017 frame1num = size(vec1, 2);0018 frame2num = size(vec2, 2);0019 0020 <span class="comment">% Vertical yellow grid line</span>0021 <span class="keyword">for</span> i = 1:frame1num,0022 line([i, i], [1, frame2num], <span class="string">'color'</span>, <span class="string">'y'</span>);0023 <span class="keyword">end</span>0024 <span class="comment">% Horizontal yellow grid line</span>0025 <span class="keyword">for</span> j = 1:frame2num,0026 line([1, frame1num], [j, j], <span class="string">'color'</span>, <span class="string">'y'</span>);0027 <span class="keyword">end</span>0028 0029 <span class="comment">% ====== Solution obtained by DTW</span>0030 <span class="keyword">for</span> i = 1:size(DTWpath,2)-1,0031 line([DTWpath(1,i), DTWpath(1,i+1)], [DTWpath(2,i), DTWpath(2,i+1)], <span class="string">'color'</span>, <span class="string">'r'</span>, <span class="string">'marker'</span>, <span class="string">'.'</span>);0032 <span class="keyword">end</span>0033 <span class="keyword">if</span> nargin==4, <span class="comment">% Another DTW path via different DTW method</span>0034 <span class="keyword">for</span> i = 1:size(DTWpath2,2)-1,0035 line([DTWpath2(1,i), DTWpath2(1,i+1)], [DTWpath2(2,i), DTWpath2(2,i+1)], <span class="string">'color'</span>, <span class="string">'k'</span>, <span class="string">'marker'</span>, <span class="string">'.'</span>);0036 <span class="keyword">end</span>0037 <span class="keyword">end</span>0038 xlabel(inputname(1));0039 ylabel(inputname(2));0040 <span class="keyword">if</span> ~isempty(DTWpath),0041 temp = vec1(:,DTWpath(1,:))-vec2(:,DTWpath(2,:));0042 dtwDist = sum(sqrt(sum(temp.^2)));0043 <span class="keyword">else</span>0044 dtwDist = inf;0045 <span class="keyword">end</span>0046 title([<span class="string">'DTW total distance = '</span>, num2str(dtwDist)]);0047 box on0048 axis tight0049 axis image0050 <span class="keyword">return</span>0051 <span class="keyword">end</span>0052 0053 vec1=vec1(:);0054 vec2=vec2(:);0055 pos = get(gcf, <span class="string">'pos'</span>);0056 width = pos(3);0057 height = pos(4);0058 <span class="comment">% Assume width > height</span>0059 <span class="comment">% Assume the aspect ratio of the signal axis is b:1</span>0060 b = 4;0061 k = height/(b+2.5);0062 0063 vec1Axis = subplot(2,2,4);0064 plot(1:length(vec1), vec1, 1:length(vec1), vec1, <span class="string">'r.'</span>);0065 axis tight0066 ylabel(<span class="string">'vec1'</span>);0067 set(vec1Axis, <span class="string">'unit'</span>, <span class="string">'pixels'</span>);0068 set(vec1Axis, <span class="string">'position'</span>, [width-(b+0.5)*k, height-(b+2)*k, b*k, k]); 0069 0070 vec2Axis = subplot(2,2,1);0071 plot(min(vec2)+max(vec2)-vec2, 1:length(vec2), min(vec2)+max(vec2)-vec2, 1:length(vec2), <span class="string">'r.'</span>);0072 axis tight;0073 xlabel(<span class="string">'vec2'</span>);0074 set(vec2Axis, <span class="string">'unit'</span>, <span class="string">'pixels'</span>);0075 set(vec2Axis, <span class="string">'position'</span>, [width-(b+2)*k, height-(b+0.5)*k, k, b*k]); 0076 <span class="comment">% The following should be done after axis is resized</span>0077 xticklabel = get(vec2Axis, <span class="string">'xticklabel'</span>);0078 set(vec2Axis, <span class="string">'xticklabel'</span>, flipud(xticklabel));0079 0080 pathAxis = subplot(2,2,2);0081 box on;0082 set(pathAxis, <span class="string">'unit'</span>, <span class="string">'pixels'</span>);0083 position = [width-(b+0.5)*k, height-(b+0.5)*k, b*k, b*k]; 0084 set(pathAxis, <span class="string">'position'</span>, position); 0085 <span class="comment">% Plot the yellow grid line</span>0086 <span class="keyword">for</span> i = 1:length(vec1),0087 line([i, i], [1, length(vec2)], <span class="string">'color'</span>, <span class="string">'y'</span>);0088 <span class="keyword">end</span>0089 <span class="keyword">for</span> i = 1:length(vec2),0090 line([1, length(vec1)], [i, i], <span class="string">'color'</span>, <span class="string">'y'</span>);0091 <span class="keyword">end</span>0092 <span class="comment">% ====== Solution obtained by DTW</span>0093 <span class="keyword">for</span> i = 1:size(DTWpath,2)-1,0094 line(DTWpath(1, i:i+1), DTWpath(2, i:i+1), <span class="string">'color'</span>, <span class="string">'r'</span>, <span class="string">'marker'</span>, <span class="string">'.'</span>);0095 <span class="keyword">end</span>0096 <span class="keyword">if</span> ~isempty(DTWpath),0097 dtwDist = sum(abs(vec1(DTWpath(1,:))-vec2(DTWpath(2,:))));0098 <span class="keyword">else</span>0099 dtwDist = inf;0100 <span class="keyword">end</span>0101 title(sprintf(<span class="string">'DTW total distance = %g'</span>, dtwDist));0102 0103 <span class="keyword">if</span> nargin==4, <span class="comment">% Another DTW path via different DTW method</span>0104 <span class="keyword">for</span> i = 1:size(DTWpath2,2)-1,0105 line(DTWpath2(1, i:i+1), DTWpath2(2, i:i+1), <span class="string">'color'</span>, <span class="string">'k'</span>, <span class="string">'marker'</span>, <span class="string">'.'</span>);0106 <span class="keyword">end</span>0107 <span class="keyword">if</span> ~isempty(DTWpath2),0108 dtwDist2 = sum(abs(vec1(DTWpath2(1,:))-vec2(DTWpath2(2,:))));0109 <span class="keyword">else</span>0110 dtwDist2 = inf;0111 <span class="keyword">end</span>0112 title(sprintf(<span class="string">'DTW total distances = %g, %g'</span>, dtwDist, dtwDist2));0113 <span class="keyword">end</span>0114 0115 axis equal0116 axis tight0117 0118 <span class="keyword">if</span> length(vec2)>length(vec1),0119 <span class="comment">% Adjust the axis of vec1</span>0120 dtwPlotWidth = position(3)*length(vec1)/length(vec2);0121 oldPos = get(vec1Axis, <span class="string">'pos'</span>);0122 position = oldPos;0123 position(1) = oldPos(1)+(oldPos(3)-dtwPlotWidth)/2;0124 position(3) = dtwPlotWidth;0125 set(vec1Axis, <span class="string">'pos'</span>, position);0126 <span class="keyword">else</span>0127 <span class="comment">% Adjust the axis of vec2</span>0128 dtwPlotHeight = position(3)*length(vec2)/length(vec1);0129 oldPos = get(vec2Axis, <span class="string">'pos'</span>);0130 position = oldPos;0131 position(2) = oldPos(2)+(oldPos(4)-dtwPlotHeight)/2;0132 position(4) = dtwPlotHeight;0133 set(vec2Axis, <span class="string">'pos'</span>, position);0134 <span class="keyword">end</span>0135 0136 set(findobj(gcf, <span class="string">'unit'</span>, <span class="string">'pixel'</span>), <span class="string">'unit'</span>, <span class="string">'normalize'</span>);0137 0138 <span class="comment">% ====== self demo ======</span>0139 <a name="_sub1" href="#_subfunctions" class="code">function selfdemo</a>0140 0141 addpath ../audioProcessing -<span class="keyword">end</span>0142 file1 = <span class="string">'waveData/singer1.wav'</span>;0143 file2 = <span class="string">'waveData/singer2.wav'</span>;0144 y1 = wavread(file1);0145 y2 = wavread(file2);0146 mfcc1 = wave2mfccmex(y1);0147 mfcc2 = wave2mfccmex(y2);0148 rmpath ../audioProcessing0149 0150 <span class="comment">% ====== Invoke DTW</span>0151 [minDist, dtwPath1] = dtw1mex(mfcc1, mfcc2);0152 fprintf(<span class="string">'dtw1mex(): total distance = %g, path length = %g\n'</span>, minDist, size(dtwPath1,2));0153 [minDist, dtwPath2] = dtw1meanDistMex(mfcc1, mfcc2);0154 fprintf(<span class="string">'dtw1meanDistMex(): average distance = %g, path length = %g\n'</span>, minDist, size(dtwPath2,2));0155 0156 <span class="comment">% ====== Plot the DTW paths</span>0157 figure;0158 feval(mfilename, mfcc1, mfcc2, dtwPath1, dtwPath2); <span class="comment">% Plot two paths</span>0159 title(<span class="string">'DTW paths (red: min. total distance, green: min. average distance)'</span>);0160 xlabel(file1); ylabel(file2);0161 0162 v1=randn(1, 25);0163 v2=randn(1, 30);0164 [minDist, dtwPath]=dtw1mex(v1, v2);0165 figure;0166 feval(mfilename, v1, v2, dtwPath);</pre></div><hr><address>Generated on Thu 30-Oct-2008 12:53:56 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/">m2html</a></strong> © 2003</address></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -