comparison adaptInstrSpecExample.m @ 0:b4e26b53072f tip

Initial commit.
author Holger Kirchhoff <holger.kirchhoff@eecs.qmul.ac.uk>
date Tue, 04 Dec 2012 13:57:15 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:b4e26b53072f
1 % This is an example script to illustrate the use of the CAdaptInstrSpec
2 % class. The script loads the spectra extracted two recordings of two
3 % different violins. It then applied the CAdaptInstrSpec class to adapt the
4 % spectra of the first violin to those of the second.
5
6 clearvars;
7 close all;
8
9 addpath('./functions/');
10
11
12 %% constants
13
14 % file paths of instrument spectra
15 noteSpectraFilePaths = {'./instrSpectra/Violin.CQT.mat'; ...
16 './instrSpectra/Violin2.CQT.mat'};
17 numInstr = length(noteSpectraFilePaths);
18
19 % analysis
20 tuningFreqInHz = 440;
21 costFctName = 'LS';
22 beta = 2; % only needed for plotting. should match the cost function above
23 numIter = 5;
24
25
26
27
28 %% load instrument spectra
29 instrSpec = cell(numInstr,1);
30 f0Idcs = cell(numInstr,1);
31
32 for instrIdx = 1:numInstr
33 load(noteSpectraFilePaths{instrIdx}); % loads variables: noteSpectra, midiPitches, freqsInHz
34 instrSpec{instrIdx} = noteSpectra;
35 f0Idcs{instrIdx} = midiPitch2Shift(midiPitches, tuningFreqInHz, freqsInHz)+1;
36 end
37 numBinsPerSemitone = getNumBinsPerSemitoneFromFreqVec(freqsInHz);
38
39
40 %% estimate filter curve
41
42 % create instrSpecFilter object
43 instrFiltObj = CAdaptInstrSpec(instrSpec{1}, instrSpec{2}, f0Idcs{1}, f0Idcs{2}, numBinsPerSemitone, costFctName);
44
45 % apply update function for h
46 for iterIdx = 1:numIter;
47 instrFiltObj = instrFiltObj.updateH;
48 end
49
50 h = instrFiltObj.getH;
51 h_smooth = instrFiltObj.getSmoothedH;
52
53
54 %% estimate spectra with given filter curve
55 estSpectra = instrFiltObj.estimateSpectra(f0Idcs{1});
56
57
58
59
60 %% plot results
61
62 % plot original spectra and estimated spectra
63 yticks = [250 500 1000 2000 4000];
64 yticklabel = {'250'; '500'; '1k'; '2k'; '4k'};
65
66 figure;
67 subplot(311);
68 imagesc(f0Idcs{1}, freqsInHz, instrSpec{1});
69 axis xy;
70 set(gca, 'YScale', 'log', 'YTick', yticks, 'YTickLabel', yticklabel);
71 xlabel('pitch index');
72 ylabel('frequency [Hz]');
73 title('spectra of instrument 1');
74
75
76 subplot(312);
77 imagesc(f0Idcs{1}, freqsInHz, instrSpec{2});
78 axis xy;
79 set(gca, 'YScale', 'log', 'YTick', yticks, 'YTickLabel', yticklabel);
80 xlabel('pitch index');
81 ylabel('frequency [Hz]');
82 title('spectra of instrument 2');
83
84 subplot(313);
85 imagesc(f0Idcs{1}, freqsInHz, estSpectra);
86 axis xy;
87 set(gca, 'YScale', 'log', 'YTick', yticks, 'YTickLabel', yticklabel);
88 xlabel('pitch index');
89 ylabel('frequency [Hz]');
90 title('spectra of instrument 2 adapted to instrument 1')
91
92
93 % plot filter curve and errors
94 xticks = [125 250 500 1000 2000 4000];
95 xticklabel = {'125'; '250'; '500'; '1k'; '2k'; '4k'};
96
97 % compute beta divergence for each element
98 betaDivsOriginal = betaDivergencePerElement(instrSpec{1}, instrSpec{2}, beta);
99 betaDivsEstimate = betaDivergencePerElement(instrSpec{1}, estSpectra, beta);
100
101 % scale per-element beta divergence for image plot
102 numColors = size(colormap,1);
103 maxDist = max([betaDivsOriginal(:); betaDivsEstimate(:)]);
104 betaDivsOriginal = betaDivsOriginal / maxDist * numColors;
105 betaDivsEstimate = betaDivsEstimate / maxDist * numColors;
106
107
108
109 figure;
110 subplot(311)
111 %stem(freqsInHz(h ~= 0), h(h ~= 0), 'k', 'filled');
112 plot(freqsInHz(h ~= 0), h(h ~= 0), 'k.');
113 hold on;
114 plot(freqsInHz, h_smooth, 'k');
115 hold off;
116 axis tight;
117 set(gca, 'XScale', 'log', 'XTick', xticks, 'XTickLabel', xticklabel);
118 title('estimated filter curve ''h''');
119 legend('original', 'smoothed', 'Location', 'NorthWest');
120
121 subplot(312);
122 image(f0Idcs{1}, freqsInHz, betaDivsOriginal);
123 axis xy;
124 set(gca, 'YScale', 'log', 'YTick', yticks, 'YTickLabel', yticklabel);
125 xlabel('pitch index');
126 ylabel('frequency [Hz]');
127 title('elementwise differences between original sets of spectra');
128
129 subplot(313);
130 image(f0Idcs{1}, freqsInHz, betaDivsEstimate);
131 axis xy;
132 set(gca, 'YScale', 'log', 'YTick', yticks, 'YTickLabel', yticklabel);
133 xlabel('pitch index');
134 ylabel('frequency [Hz]');
135 title('differences between original and adapted set of spectra');