Mercurial > hg > camir-aes2014
comparison core/magnatagatune/DistMeasureNNet.m @ 0:e9a9cd732c1e tip
first hg version after svn
author | wolffd |
---|---|
date | Tue, 10 Feb 2015 15:05:51 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e9a9cd732c1e |
---|---|
1 % --- | |
2 % The DistMeasureMahal class states a wrapper for | |
3 % special Mahalanobis similarity distance, and is compatible with the | |
4 % DistMeasure class | |
5 % --- | |
6 classdef DistMeasureNNet < handle | |
7 | |
8 properties (SetAccess = private) | |
9 | |
10 net; | |
11 | |
12 featX; | |
13 | |
14 ids; | |
15 ret_ids; | |
16 end | |
17 | |
18 methods | |
19 | |
20 % --- | |
21 % constructor | |
22 % --- | |
23 function m = DistMeasureNNet(clips, net, featX) | |
24 | |
25 if size(featX, 2) ~= numel(clips) | |
26 error 'wrong input format' | |
27 end | |
28 | |
29 % fill index and generate matrix; | |
30 m.ids = [clips.id]; | |
31 | |
32 % reverse index | |
33 m.ret_ids = sparse(numel(m.ids),1); | |
34 m.ret_ids(m.ids) = 1:numel(m.ids); | |
35 | |
36 % --- | |
37 % save neural net and lazy-copy features | |
38 % --- | |
39 m.net = net; | |
40 | |
41 m.featX = featX; | |
42 | |
43 end | |
44 | |
45 | |
46 % --- | |
47 % this function returns the | |
48 % similarity of two clip indices | |
49 % --- | |
50 function out = mat(m, idxa, idxb) | |
51 | |
52 if nargin == 1 | |
53 idxa = 1:numel(m.ids); | |
54 idxb = 1:numel(m.ids); | |
55 end | |
56 | |
57 % cycle through all index combinations | |
58 out = zeros(numel(idxa), numel(idxb)); | |
59 for i = 1:numel(idxa) | |
60 for j = 1:numel(idxb) | |
61 | |
62 % calculate distance vector | |
63 deltas = m.featX(:,idxa(i)) - m.featX(:,idxb(j)); | |
64 | |
65 % return distance from net | |
66 out(i,j) = m.net.calcValue(deltas); | |
67 end | |
68 end | |
69 | |
70 end | |
71 | |
72 % --- | |
73 % returns the distance for the two input clips | |
74 % --- | |
75 function out = distance(m, clipa, clipb) | |
76 posa = m.get_clip_pos(clipa); | |
77 posb = m.get_clip_pos(clipb); | |
78 | |
79 out = m.mat(posa, posb); | |
80 end | |
81 | |
82 % --- | |
83 % returns a list of n (default = 10) clips most | |
84 % similar to the input | |
85 % --- | |
86 function [clips, dist] = get_nearest(m, clip, n) | |
87 % list = get_nearest(m, clip, n) | |
88 % | |
89 % returns a list of n (default = 10) clips most | |
90 % similar to the input | |
91 | |
92 % default number of results | |
93 if nargin == 2 | |
94 | |
95 n = 10; | |
96 end | |
97 | |
98 % return all clips in case n = 0 | |
99 if n == 0; n = numel(m.ids); end | |
100 | |
101 % get clip positions | |
102 pos = m.get_clip_pos(clip); | |
103 | |
104 % sort according to distance | |
105 [sc, idx] = sort( m.mat(pos, 1:numel(m.ids)), 'ascend'); | |
106 | |
107 % we only output relevant data | |
108 idx = idx(sc < inf); | |
109 | |
110 if numel(idx) > 0 | |
111 % create clips form best ids | |
112 clips = MTTClip( m.ids( idx(1:min(n, end)))); | |
113 dist = m.mat(pos, idx(1:min(n, end))); | |
114 | |
115 else | |
116 clips = []; | |
117 dist = []; | |
118 end | |
119 end | |
120 | |
121 | |
122 | |
123 function [clips, dist] = present_nearest(m, clip, n) | |
124 % plays and shows the n best hits for a given clip | |
125 | |
126 % default number of results | |
127 if nargin == 2 | |
128 | |
129 n = 3; | |
130 end | |
131 | |
132 % get best list | |
133 [clips, dist] = get_nearest(m, clip, n); | |
134 | |
135 clip.audio_features_basicsm.visualise(); | |
136 for i = 1:numel(clips) | |
137 fprintf('\n\n\n- Rank %d, distance: %1.4f \n\n',i, dist(i)); | |
138 | |
139 clips(i).audio_features_basicsm.visualise(); | |
140 h = gcf(); | |
141 t = clips(i).play(20); | |
142 pause(t); | |
143 close(h); | |
144 end | |
145 end | |
146 | |
147 function a = visualise(m) | |
148 | |
149 figure; | |
150 | |
151 % plot data | |
152 | |
153 imagesc(m.mat); | |
154 | |
155 a = gca; | |
156 set(a,'YTick',[1:numel(m.ids)], 'YTickLabel',m.ids); | |
157 set(a,'XTick',[1:numel(m.ids)], 'XTickLabel', m.ids); | |
158 | |
159 axis xy; | |
160 colormap(hot); | |
161 end | |
162 | |
163 % end methods | |
164 end | |
165 | |
166 % --- | |
167 % private methods | |
168 % --- | |
169 methods(Access = private) | |
170 | |
171 function out = get_clip_pos(m, clip) | |
172 % returns position in mat for given clip | |
173 | |
174 out = m.ret_ids(clip.id); | |
175 end | |
176 | |
177 end | |
178 | |
179 end |