Mercurial > hg > nimfks
comparison src/matlab/istft.m @ 0:c52bc3e8d3ad tip
user: boblsturm
branch 'default'
added README.md
added assets/.DS_Store
added assets/playButton.jpg
added assets/stopButton.png
added assets/swapButton.jpg
added data/.DS_Store
added data/fiveoctaves.mp3
added data/glock2.wav
added data/sinScale.mp3
added data/speech_female.mp3
added data/sweep.wav
added nimfks.m.lnk
added src/.DS_Store
added src/matlab/.DS_Store
added src/matlab/AnalysisCache.m
added src/matlab/CSS.m
added src/matlab/DataHash.m
added src/matlab/ExistsInCache.m
added src/matlab/KLDivCost.m
added src/matlab/LoadFromCache.m
added src/matlab/SA_B_NMF.m
added src/matlab/SaveInCache.m
added src/matlab/Sound.m
added src/matlab/SynthesisCache.m
added src/matlab/chromagram_E.m
added src/matlab/chromagram_IF.m
added src/matlab/chromagram_P.m
added src/matlab/chromsynth.m
added src/matlab/computeSTFTFeat.m
added src/matlab/controller.m
added src/matlab/decibelSliderReleaseCallback.m
added src/matlab/drawClickCallBack.m
added src/matlab/fft2chromamx.m
added src/matlab/hz2octs.m
added src/matlab/ifgram.m
added src/matlab/ifptrack.m
added src/matlab/istft.m
added src/matlab/nimfks.fig
added src/matlab/nimfks.m
added src/matlab/nmfFn.m
added src/matlab/nmf_beta.m
added src/matlab/nmf_divergence.m
added src/matlab/nmf_euclidean.m
added src/matlab/prune_corpus.m
added src/matlab/rot_kernel.m
added src/matlab/templateAdditionResynth.m
added src/matlab/templateDelCb.m
added src/matlab/templateScrollCb.m
author | boblsturm |
---|---|
date | Sun, 18 Jun 2017 06:26:13 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c52bc3e8d3ad |
---|---|
1 function y = istft(spec,parameter, varargin) | |
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
3 % Name: istft | |
4 % Date: 03-2014 | |
5 % Programmer: Jonathan Driedger | |
6 % http://www.audiolabs-erlangen.de/resources/MIR/TSMtoolbox/ | |
7 % | |
8 % Computing the 'inverse' of the stft according to the paper "Signal | |
9 % Estimation from Modified Short-Time Fourier Transform" by Griffin and | |
10 % Lim. | |
11 % | |
12 % Input: spec a complex spectrogram generated by stft. | |
13 % parameter. | |
14 % synHop hop size of the synthesis window. | |
15 % win the synthesis window. | |
16 % zeroPad number of zeros that were padded to the | |
17 % window to increase the fft size and therefore | |
18 % the frequency resolution. | |
19 % numOfIter number of iterations the algorithm should | |
20 % perform to adapt the phase. | |
21 % origSigLen original length of the audio signal such that | |
22 % the output can be trimmed accordingly. | |
23 % restoreEnergy when windowing the synthesis frames, there is a | |
24 % potential for some energy loss. This option | |
25 % will rescale every windowed synthesis frame to | |
26 % compensate for this energy leakage. | |
27 % fftShift in case the stft was computed with an fftShift, | |
28 % setting this parameter to 1 will compensate for | |
29 % that by applying the same shifting operation | |
30 % again to each frame after the ifft. | |
31 % | |
32 % Output: y the time-domain signal. | |
33 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
34 % Reference: | |
35 % If you use the 'TSM toolbox' please refer to: | |
36 % [DM14] Jonathan Driedger, Meinard Mueller | |
37 % TSM Toolbox: MATLAB Implementations of Time-Scale Modification | |
38 % Algorithms | |
39 % Proceedings of the 17th International Conference on Digital Audio | |
40 % Effects, Erlangen, Germany, 2014. | |
41 % | |
42 % License: | |
43 % This file is part of 'TSM toolbox'. | |
44 % | |
45 % 'TSM toolbox' is free software: you can redistribute it and/or modify it | |
46 % under the terms of the GNU General Public License as published by the | |
47 % the Free Software Foundation, either version 3 of the License, or (at | |
48 % your option) any later version. | |
49 % | |
50 % 'TSM toolbox' is distributed in the hope that it will be useful, but | |
51 % WITHOUT ANY WARRANTY; without even the implied warranty of | |
52 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | |
53 % Public License for more details. | |
54 % | |
55 % You should have received a copy of the GNU General Public License along | |
56 % with 'TSM toolbox'. If not, see http://www.gnu.org/licenses/. | |
57 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
58 | |
59 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
60 % check parameters | |
61 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
62 if nargin<2 | |
63 parameter=[]; | |
64 end | |
65 | |
66 if ~isfield(parameter,'synHop') | |
67 parameter.synHop = 2048; | |
68 end | |
69 if ~isfield(parameter,'win') | |
70 parameter.win = win(4096,2); % hann window | |
71 end | |
72 if ~isfield(parameter,'zeroPad') | |
73 parameter.zeroPad = 0; | |
74 end | |
75 if ~isfield(parameter,'numOfIter') | |
76 parameter.numOfIter = 1; | |
77 end | |
78 if ~isfield(parameter,'origSigLen') | |
79 parameter.origSigLen = -1; % no trimming | |
80 end | |
81 if ~isfield(parameter,'restoreEnergy') | |
82 parameter.restoreEnergy = 0; | |
83 end | |
84 if ~isfield(parameter,'fftShift') | |
85 parameter.fftShift = 0; | |
86 end | |
87 | |
88 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
89 % some pre calculations | |
90 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
91 numOfFrames = size(spec,2); | |
92 numOfIter = parameter.numOfIter; | |
93 | |
94 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
95 % audio calculation | |
96 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
97 % first iteration | |
98 Yi = spec; | |
99 yi = LSEE_MSTFT(Yi,parameter); | |
100 | |
101 % remaining iterations | |
102 parStft.win = parameter.win; | |
103 parStft.numOfFrames = numOfFrames; | |
104 parStft.zeroPad = parameter.zeroPad; | |
105 parStft.anaHop = parameter.synHop; | |
106 for j = 2 : numOfIter | |
107 Yi = abs(spec) .* exp(1j*angle(stft(yi,parStft))); | |
108 yi = LSEE_MSTFT(Yi,parameter); | |
109 end | |
110 y = yi'; | |
111 y = y./max(y); | |
112 | |
113 % if the original Length of the signal is known, also remove the zero | |
114 % padding at the end | |
115 if parameter.origSigLen > 0 | |
116 y = y(1:parameter.origSigLen); | |
117 end | |
118 | |
119 end | |
120 | |
121 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
122 % the Griffin Lim procedure | |
123 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
124 function x = LSEE_MSTFT(X,parameter) | |
125 % some pre calculations | |
126 w = parameter.win; | |
127 w = w(:); | |
128 zp = parameter.zeroPad; | |
129 w = [zeros(floor(zp/2),1);w;zeros(floor(zp/2),1)]; | |
130 winLen = length(w); | |
131 winLenHalf = round(winLen/2); | |
132 synHop = parameter.synHop; | |
133 numOfFrames = size(X,2); | |
134 winPos = (0:numOfFrames-1) * synHop + 1; | |
135 signalLength = winPos(end) + winLen - 1; | |
136 | |
137 x = zeros(signalLength,1); % resynthesized signal | |
138 ow = zeros(signalLength,1); % sum of the overlapping windows | |
139 for i = 1 : numOfFrames | |
140 currSpec = X(:,i); | |
141 | |
142 % add the conjugate complex symmetric upper half of the spectrum | |
143 Xi = [currSpec;conj(currSpec(end-1:-1:2))]; | |
144 xi = real(ifft(Xi)); | |
145 if parameter.fftShift == 1 | |
146 xi = fftshift(xi); | |
147 end | |
148 xiw = xi .* w; | |
149 | |
150 if parameter.restoreEnergy == 1 | |
151 xiEnergy = sum(abs(xi)); | |
152 xiwEnergy = sum(abs(xiw)); | |
153 xiw = xiw * (xiEnergy/(xiwEnergy+eps)); | |
154 end | |
155 | |
156 x(winPos(i):winPos(i)+winLen-1) = ... | |
157 x(winPos(i):winPos(i)+winLen-1) + xiw; | |
158 | |
159 ow(winPos(i):winPos(i)+winLen-1) = ... | |
160 ow(winPos(i):winPos(i)+winLen-1) + w.^2; | |
161 end | |
162 ow(ow<10^-3) = 1; % avoid potential division by zero | |
163 x = x ./ ow; | |
164 | |
165 % knowing the zeropads that were added in the stft computation, we can | |
166 % remove them again now. But since we do not know exactly how many | |
167 % zeros were padded at the end of the signal, it is only safe to remove | |
168 % winLenHalf zeros. | |
169 x = x(winLenHalf+1:end-winLenHalf); | |
170 end |