annotate toolboxes/MIRtoolbox1.3.2/MIRToolbox/@mircepstrum/mircepstrum.m @ 0:cc4b1211e677 tip

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