Mercurial > hg > pmhd
comparison extractInstrumental.m @ 10:6840f77b83aa
commit
author | Yading Song <yading.song@eecs.qmul.ac.uk> |
---|---|
date | Sun, 21 Apr 2013 10:55:35 +0200 |
parents | |
children | 80a9556123da |
comparison
equal
deleted
inserted
replaced
9:ed610a0bbf83 | 10:6840f77b83aa |
---|---|
1 function [y] = extractInstrumental(x,w,N,melodyFile) | |
2 %e.g. y = extractInstrumental(x,hamming(2025),4096,'baby.txt'); | |
3 | |
4 | |
5 % Initialize | |
6 M = length(w); % analysis window size | |
7 Ns = 1024; % FFT size for synthesis | |
8 H = 256; % hop size for analysis and synthesis | |
9 soundlength = length(x); % length of input sound array | |
10 hNs = Ns/2; % half synthesis window size | |
11 hM = (M-1)/2; % half analysis window size | |
12 pin = max(hNs+1,1+hM); % initialize sound pointer to middle of analysis window | |
13 pend = soundlength-max(hM,hNs); % last sample to start a frame | |
14 fftbuffer = zeros(N,1); % initialize buffer for FFT | |
15 y = zeros(soundlength+Ns/2,1); % output sine component | |
16 w = w/sum(w); % normalize analysis window | |
17 sw = zeros(Ns,1); | |
18 ow = triang(2*H-1); % overlapping window | |
19 ovidx = Ns/2+1-H+1:Ns/2+H; % overlap indexes | |
20 sw(ovidx) = ow(1:2*H-1); | |
21 bh = blackmanharris(Ns); % synthesis window | |
22 bh = bh ./ sum(bh); % normalize synthesis window | |
23 sw(ovidx) = sw(ovidx) ./ bh(ovidx); | |
24 | |
25 | |
26 % Load melody file | |
27 melody = loadMelodyFile(melodyFile); | |
28 melody = [melody zeros(2,1000)]; | |
29 i=0; | |
30 | |
31 % For each segment | |
32 while pin<pend | |
33 | |
34 i=i+1; | |
35 | |
36 % Find predominant pitch for segment | |
37 melody_seg = melody(:,ceil((pin-hM)/128):round((pin+hM)/128)); | |
38 ind = find(melody_seg(2,:)>150); % Find pitches larger than 150Hz | |
39 medpitch = median(melody_seg(2,ind)); % Median pitch in segment | |
40 | |
41 | |
42 % Compute FFT for segment | |
43 xw = x(pin-hM:pin+hM).*w(1:M); % window the input sound | |
44 fftbuffer(1:(M+1)/2) = xw((M+1)/2:M); % zero-phase window in fftbuffer | |
45 fftbuffer(N-(M-1)/2+1:N) = xw(1:(M-1)/2); | |
46 X = fft(fftbuffer); % compute the FFT | |
47 | |
48 | |
49 % Keep only the melody for each segment | |
50 if(~isempty(ind)) % if there is melody, remove the melodic bins | |
51 | |
52 melodyBinsStart = (medpitch/10.6568) * [1:40]; | |
53 melodyBinsEnd = N + 2 - ((medpitch/10.6568) * [1:40]); | |
54 melodyBins = round([melodyBinsStart melodyBinsEnd]); | |
55 melodyBins = [melodyBins (melodyBins-1) (melodyBins+1) (melodyBins-2) (melodyBins+2) (melodyBins-3) (melodyBins+3) (melodyBins-4) (melodyBins+4) (melodyBins-5) (melodyBins+5) (melodyBins-6) (melodyBins+6)]; | |
56 | |
57 X(melodyBins) = 0; | |
58 | |
59 end; | |
60 | |
61 | |
62 ri= pin-hNs; % input sound pointer for residual analysis | |
63 yw = ifft(X); | |
64 y(ri:ri+Ns-1) = y(ri:ri+Ns-1)+yw(1:Ns).*sw; | |
65 pin = pin+H; | |
66 | |
67 end | |
68 | |
69 y = (max(x)/max(y))*y; % scale y to original amplitude | |
70 | |
71 %wavwrite(y,44100,'test.wav'); |