comparison toolboxes/MIRtoolbox1.3.2/MIRToolboxDemos/demo1pitch.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 function demo1pitch
2
3 % Demo 1:Pitch extraction
4 % Aims: To study the diverse tools available in the toolbox for pitch
5 %extraction.
6 % In the following exercises, we will try to extract the pitch contents of various audio files using diverse techniques. All the audio files are included in the ?MIRtoolbox_Demos? folder.
7
8 %% 1. Analyis of a single chord
9
10 % Load the audio file 'Amin3.wav'.
11 a = miraudio('Amin3')
12 %and play it:
13 mirplay(a)
14
15 % Observe the periodicities contained in the signal by computing the autocorrelation function:
16 ac = mirautocor(a)
17 % The autocorrelation function can also be displayed in the frequency domain (?Freq?), and the frequency range can be specified, for instance between 75 and 2400 Hz:
18 ac = mirautocor(ac, 'Freq','Min',75,'Hz','Max',2400,'Hz')
19
20 % The peaks of the curve indicates the most important periodicities:
21 pac = mirpeaks(ac)
22 % But the two peaks at the start and end of the curves are meaningless, so should be removed:
23 pac = mirpeaks(ac, 'NoBegin','NoEnd')
24
25 % The corresponding pitch height are given by:
26 p = mirpitch(pac)
27
28 % The actual numerical data (here, the frequency of each pitch) is given by:
29 mirgetdata(p)
30
31 % The resulting pitches can be finally played:
32 mirplay(p)
33
34 % So far, the results do not seem very convincing, do they?
35
36 pause, close all
37
38 %% 2. Improving the analysis
39
40 % Clearly, we need to refine the methods in order to retrieve the notes played in the chord. First of all, the autocorrelation function can be ?generalized?, i.e., ?compressed? by a factor of, for instance, 0.5 (cf. MIRtoolbox User?s Manual at the mirautocor section for an explanation of the 'Compres' option):
41 ac = mirautocor(a,'Compres',.5)
42
43 % The rest of the computation can actually be performed using the shortcut:
44 [p pac] = mirpitch(ac)
45
46 % Look and listen to the results.
47 mirgetdata(p)
48 mirplay(p)
49
50 % Does it sound better? What is the problem this time?
51
52 pause, close all
53
54 %% 3. Improving the analysis, again
55
56 % In fact, the autocorrelation function contains a lot of harmonics that need to be removed. For that purpose, the autocorrelation function can be ?enhanced? (cf. MIRtoolbox User?s Manual at the mirautocor section for an explanation of the 'Enhanced' option):
57 ac = mirautocor(ac,'Enhanced')
58
59 % Carry out the rest of the computation as in section 2.2.
60 [p pac] = mirpitch(ac)
61 mirgetdata(p)
62 mirplay(p)
63
64 % What do you think of the results?
65
66 pause, close all
67
68 %% 4. Improving the analysis, still
69
70 % An additional improvement consists in first decomposing the audio into two channels using the command:
71 fb = mirfilterbank(a,'2Channels')
72
73 % Compute the autocorrelation on each channel:
74 ac = mirautocor(fb,'Compres',.5)
75
76 % The autocorrelation of each channel can then be summed together:
77 ac = mirsum(ac)
78
79 % And the enhancement can be performed afterwards:
80 ac = mirautocor(ac,'Enhanced')
81
82 % And the rest of the computation follows the same principle than before.
83 [p ac] = mirpitch(ac)
84
85 % The result should be better this way.
86 mirgetdata(p)
87 mirplay(p)
88
89 % Hopefully, the whole chain of operation can be performed automatically using the simple command:
90 [p ac] = mirpitch(a)
91 [p ac] = mirpitch(a,'Cepstrum')
92 [p ac] = mirpitch(a,'AutocorSpectrum')
93
94 pause, close all
95
96 %% 5. Frame-based analysis
97
98 % Let?s analyze a longer musical sequence, such as ragtime.wav:
99 a = miraudio('ragtime');
100 mirplay(a)
101
102 % A direct analysis of the recording using the previous command
103 [p ac] = mirpitch(a)
104 % is not concluding, as it tries to find the pitches contained in the whole signal.
105 mirgetdata(p)
106 mirplay(p)
107
108 % It is better to estimate the pitch content frame-by-frame:
109 [p ac] = mirpitch(a,'Frame')
110 mirgetdata(p)
111
112 % Don?t forget to listen to the results.
113 mirplay(p)
114
115 pause, close all
116
117 %% 6. Segment-based analysis
118
119 % But it might get more sense to first decompose the signal into notes, using the commands:
120 o = mironsets(a,'Attacks')
121 % (The 'Attacks' option enables to segment at the beginning of each note
122 % attack phase, i.e., where the energy is minimum, instead of segmenting where the energy is maximum.)
123 mirplay(o)
124
125 sg = mirsegment(a,o)
126 mirplay(sg)
127
128 % and to compute the pitch on each successive segment:
129 [p ac] = mirpitch(sg)
130 mirgetdata(p)
131
132 % Again, don?t forget to listen to the results.
133 mirplay(p)
134
135 pause, close all
136
137 %% 7. Monody analysis
138
139 % Conclude with the analysis of a Finnish folk song, Läksin minä kesäyönä, whose excerpt is recorded in the file ?laksin.wav?. As the melody is sung by one voice only, you can use the ?Mono? option in mirpitch.
140 mirplay('laksin')
141 o = mironsets('laksin','Attacks','Contrast',.1)
142 mirplay(o)
143 sg = mirsegment('laksin',o)
144 mirplay(sg)
145 p = mirpitch(sg,'mono')
146 mirgetdata(p)
147 mirplay(p)