Mercurial > hg > camir-aes2014
comparison toolboxes/MIRtoolbox1.3.2/MIRToolbox/@mircepstrum/mircepstrum.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 varargout = mircepstrum(orig,varargin) | |
2 % s = mircepstrum(x) computes the cepstrum, which indicates | |
3 % periodicities, and is used for instance for pitch detection. | |
4 % x can be either a spectrum, an audio signal, or the name of an audio file. | |
5 % Optional parameter: | |
6 % mircepstrum(...,'Min',min) specifies the lowest delay taken into | |
7 % consideration, in seconds. | |
8 % Default value: 0.0002 s (corresponding to a maximum frequency of | |
9 % 5 kHz). | |
10 % mircepstrum(...,'Max',max) specifies the highest delay taken into | |
11 % consideration, in seconds. | |
12 % Default value: 0.05 s (corresponding to a minimum frequency of | |
13 % 20 Hz). | |
14 % mircepstrum(...,'Freq') represents the cepstrum in the frequency | |
15 % domain. | |
16 | |
17 mi.key = 'Min'; | |
18 mi.type = 'Integer'; | |
19 mi.default = 0.0002; | |
20 mi.unit = {'s','Hz'}; | |
21 mi.defaultunit = 's'; | |
22 mi.opposite = 'ma'; | |
23 option.mi = mi; | |
24 | |
25 ma.key = 'Max'; | |
26 ma.type = 'Integer'; | |
27 ma.default = .05; | |
28 ma.unit = {'s','Hz'}; | |
29 ma.defaultunit = 's'; | |
30 ma.opposite = 'mi'; | |
31 option.ma = ma; | |
32 | |
33 fr.key = 'Freq'; | |
34 fr.type = 'Boolean'; | |
35 fr.default = 0; | |
36 option.fr = fr; | |
37 | |
38 complex.key = 'Complex'; | |
39 complex.type = 'Boolean'; | |
40 complex.default = 0; | |
41 option.complex = complex; | |
42 | |
43 specif.option = option; | |
44 | |
45 specif.defaultframelength = 0.05; | |
46 specif.defaultframehop = 0.5; | |
47 | |
48 varargout = mirfunction(@mircepstrum,orig,varargin,nargout,specif,@init,@main); | |
49 | |
50 | |
51 function [x type] = init(x,option) | |
52 if not(isamir(x,'mircepstrum')) | |
53 x = mirspectrum(x); | |
54 end | |
55 type = 'mircepstrum'; | |
56 | |
57 | |
58 function c = main(orig,option,postoption) | |
59 if iscell(orig) | |
60 orig = orig{1}; | |
61 end | |
62 c.phase = []; | |
63 if isa(orig,'mircepstrum') | |
64 c.freq = orig.freq; | |
65 else | |
66 c.freq = 0; | |
67 end | |
68 c = class(c,'mircepstrum',mirdata(orig)); | |
69 c = purgedata(c); | |
70 c = set(c,'Title','Cepstrum','Abs','quefrency (s)','Ord','magnitude'); | |
71 | |
72 if isa(orig,'mircepstrum') | |
73 if option.ma < Inf || option.mi > 0 || get(orig,'FreqDomain') | |
74 mag = get(orig,'Magnitude'); | |
75 pha = get(orig,'Phase'); | |
76 que = get(orig,'Quefrency'); | |
77 for h = 1:length(mag) | |
78 for k = 1:length(mag{h}) | |
79 if get(orig,'FreqDomain') | |
80 mag{h}{k} = flipud(mag{h}{k}); | |
81 que{h}{k} = flipud(1./que{h}{k}); | |
82 pha{h}{k} = flipud(pha{h}{k}); | |
83 end | |
84 range = find(que{h}{k}(:,1,1) <= option.ma & ... | |
85 que{h}{k}(:,1,1) >= option.mi); | |
86 mag{h}{k} = mag{h}{k}(range,:,:); | |
87 pha{h}{k} = pha{h}{k}(range,:,:); | |
88 que{h}{k} = que{h}{k}(range,:,:); | |
89 end | |
90 end | |
91 c = set(c,'Magnitude',mag,'Phase',pha,'Quefrency',que,'FreqDomain',0); | |
92 end | |
93 c = modif(c,option); | |
94 elseif isa(orig,'mirspectrum') | |
95 mag = get(orig,'Magnitude'); | |
96 pha = get(orig,'Phase'); | |
97 f = get(orig,'Sampling'); | |
98 q = cell(1,length(mag)); | |
99 for h = 1:length(mag) | |
100 len = ceil(option.ma*f{h}); | |
101 start = ceil(option.mi*f{h})+1; | |
102 q{h} = cell(1,length(mag{h})); | |
103 for k = 1:length(mag{h}) | |
104 m = mag{h}{k}.*exp(1i*pha{h}{k}); | |
105 m = [m(1:end-1,:) ; conj(flipud(m))]; % Reconstitution of the complete abs(FFT) | |
106 if not(option.complex) | |
107 m = abs(m); | |
108 end | |
109 m = log(m); | |
110 c0=fft(m); | |
111 q0=repmat((0:(size(c0,1)-1))'/f{k},[1,size(m,2),size(m,3)]); | |
112 len = min(len,floor(size(c0,1)/2)); | |
113 mag{h}{k} = abs(c0(start:len,:,:)); | |
114 if option.complex | |
115 pha{h}{k} = unwrap(angle(c0(start:len,:,:))); | |
116 else | |
117 pha{h}{k} = nan(size(c0(start:len,:,:))); | |
118 end | |
119 q{h}{k} = q0(start:len,:,:); | |
120 end | |
121 end | |
122 c = set(c,'Magnitude',mag,'Phase',pha,'Quefrency',q); | |
123 c = modif(c,option); | |
124 end | |
125 | |
126 | |
127 function c = modif(c,option) | |
128 mag = get(c,'Magnitude'); | |
129 que = get(c,'Quefrency'); | |
130 if option.fr && not(get(c,'FreqDomain')) | |
131 for k = 1:length(mag) | |
132 for l = 1:length(mag{k}) | |
133 m = mag{k}{l}; | |
134 q = que{k}{l}; | |
135 if not(isempty(m)) | |
136 if q(1,1) == 0 | |
137 m = m(2:end,:,:); | |
138 q = q(2:end,:,:); | |
139 end | |
140 m = flipud(m); | |
141 q = flipud(1./q); | |
142 end | |
143 mag{k}{l} = m; | |
144 que{k}{l} = q; | |
145 end | |
146 end | |
147 c = set(c,'FreqDomain',1,'Abs','frequency (Hz)'); | |
148 end | |
149 c = set(c,'Magnitude',mag,'Quefrency',que,'Freq'); |