boblsturm@0
|
1 function wts = fft2chromamx(nfft,nbins,sr,A440,ctroct,octwidth)
|
boblsturm@0
|
2 % wts = fft2chromamx(nfft,nbins,sr,A440,ctroct,octwidth)
|
boblsturm@0
|
3 % Create a wts matrix to convert FFT to Chroma
|
boblsturm@0
|
4 % A440 is optional ref frq for A
|
boblsturm@0
|
5 % ctroct, octwidth specify a dominance window - Gaussian
|
boblsturm@0
|
6 % weighting centered on ctroct (in octs, re A0 = 27.5Hz) and
|
boblsturm@0
|
7 % with a gaussian half-width of octwidth. Defaults to
|
boblsturm@0
|
8 % halfwidth = inf i.e. flat.
|
boblsturm@0
|
9 % 2006-06-29 dpwe@ee.columbia.edu
|
boblsturm@0
|
10
|
boblsturm@0
|
11 if nargin < 2; nbins = 12; end
|
boblsturm@0
|
12 if nargin < 3; sr = 22050; end
|
boblsturm@0
|
13 if nargin < 4; A440 = 440; end
|
boblsturm@0
|
14 if nargin < 5; ctroct = 5; end
|
boblsturm@0
|
15 if nargin < 6; octwidth = 0; end
|
boblsturm@0
|
16
|
boblsturm@0
|
17 wts = zeros(nbins, nfft);
|
boblsturm@0
|
18
|
boblsturm@0
|
19 fftfrqbins = nbins*hz2octs([1:(nfft-1)]/nfft*sr,A440);
|
boblsturm@0
|
20
|
boblsturm@0
|
21 % make up a value for the 0 Hz bin = 1.5 octaves below bin 1
|
boblsturm@0
|
22 % (so chroma is 50% rotated from bin 1, and bin width is broad)
|
boblsturm@0
|
23 fftfrqbins = [fftfrqbins(1)-1.5*nbins,fftfrqbins];
|
boblsturm@0
|
24
|
boblsturm@0
|
25 binwidthbins = [max(1, fftfrqbins(2:nfft) - fftfrqbins(1:(nfft-1))), 1];
|
boblsturm@0
|
26
|
boblsturm@0
|
27 D = repmat(fftfrqbins,nbins,1) - repmat([0:(nbins-1)]',1,nfft);
|
boblsturm@0
|
28
|
boblsturm@0
|
29 nbins2 = round(nbins/2);
|
boblsturm@0
|
30
|
boblsturm@0
|
31 % Project into range -nbins/2 .. nbins/2
|
boblsturm@0
|
32 % add on fixed offset of 10*nbins to ensure all values passed to rem are +ve
|
boblsturm@0
|
33 D = rem(D + nbins2 + 10*nbins, nbins) - nbins2;
|
boblsturm@0
|
34
|
boblsturm@0
|
35 % Gaussian bumps - 2*D to make them narrower
|
boblsturm@0
|
36 wts = exp(-0.5*(2*D./repmat(binwidthbins,nbins,1)).^2);
|
boblsturm@0
|
37
|
boblsturm@0
|
38 % normalize each column
|
boblsturm@0
|
39 wts = wts./repmat(sqrt(sum(wts.^2)),nbins,1);
|
boblsturm@0
|
40
|
boblsturm@0
|
41 % remove aliasing columns
|
boblsturm@0
|
42 wts(:,[(nfft/2+2):nfft]) = 0;
|
boblsturm@0
|
43
|
boblsturm@0
|
44 % Maybe apply scaling for fft bins
|
boblsturm@0
|
45 if octwidth > 0
|
boblsturm@0
|
46 wts = wts.*repmat(exp(-0.5*(((fftfrqbins/nbins - ctroct)/octwidth).^2)), nbins, 1);
|
boblsturm@0
|
47 end
|
boblsturm@0
|
48
|
boblsturm@0
|
49 %wts = binwidthbins;
|
boblsturm@0
|
50 %wts = fftfrqbins;
|
boblsturm@0
|
51
|
boblsturm@0
|
52 function octs = hz2octs(freq, A440)
|
boblsturm@0
|
53 % octs = hz2octs(freq, A440)
|
boblsturm@0
|
54 % Convert a frequency in Hz into a real number counting
|
boblsturm@0
|
55 % the octaves above A0. So hz2octs(440) = 4.0
|
boblsturm@0
|
56 % Optional A440 specifies the Hz to be treated as middle A (default 440).
|
boblsturm@0
|
57 % 2006-06-29 dpwe@ee.columbia.edu for fft2chromamx
|
boblsturm@0
|
58
|
boblsturm@0
|
59 %if nargin < 2; A440 = 440; end
|
boblsturm@0
|
60
|
boblsturm@0
|
61 % A4 = A440 = 440 Hz, so A0 = 440/16 Hz
|
boblsturm@0
|
62 octs = log(freq./(A440/16))./log(2);
|
boblsturm@0
|
63
|
boblsturm@0
|
64
|