-
Notifications
You must be signed in to change notification settings - Fork 0
/
veritune.m
123 lines (78 loc) · 2.73 KB
/
veritune.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
function [outputVector] = veritune(inputVector, st)
%scaling
step = 2*st;
%pitch scaling factor
alpha = 2*step;
%space between windows, they have as 256
hop = 256;
hopOut = round(alpha*hop);
x = inputVector;
windowSize = 1024;
% Hanning window for overlap-add
wn = hann(windowSize*2+1);
wn = wn(2:2:end);
%---------------------First part: creating frams----------------------------------------
%outputs vectorFrames and num_slices
%Max number of slices that can be obtained: Rounded! (length of input -
%window size) / hop
num_slices = floor((length(x) - windowSize) / hop);
% local changing of the source file to truncate and make sure only integer # of hop
x = x(1:(((num_slices*hop)) + windowSize));
vectorFrames = zeros(1020,1024);
% Get vectorFrames
for index = 1:num_slices
indexTimeStart = (index-1)*hop + 1;
indexTimeEnd = (index-1)*hop + windowSize;
vectorFrames(index,:) = x(indexTimeStart: indexTimeEnd);
end
outputy = zeros(1020,1024);
% Initialize cumulative phase
phaseCumulative = 0;
% Initialize previous frame phase
previousPhase = 0;
for index=1:num_slices
%ANALYSIS
%get current frame
currentFrame = vectorFrames(index,:);
%window the frame!
currentFrameWindowed = currentFrame .* wn' / sqrt(((windowSize/hop)/2));
%fft
currentFrameWindowedFFT = fft(currentFrameWindowed);
%get magnitude
magFrame = abs(currentFrameWindowedFFT);
phaseFrame = angle(currentFrameWindowedFFT);
% Get the phase difference
deltaPhi = phaseFrame - previousPhase;
previousPhase = phaseFrame;
% Remove the expected phase difference
deltaPhiPrime = deltaPhi - hop * 2*pi*(0:(windowSize-1))/windowSize;
% Map to -pi/pi range
deltaPhiPrimeMod = mod(deltaPhiPrime+pi, 2*pi) - pi;
% Get the true frequency
trueFreq = 2*pi*(0:(windowSize-1))/windowSize + deltaPhiPrimeMod/hop;
% Get the final phase
phaseCumulative = phaseCumulative + hopOut * trueFreq;
outputFrame = real(ifft(magFrame .* exp(1i*phaseCumulative)));
outputy(index,:) = outputFrame .* wn' / sqrt((windowSize/hopOut)/2);
end
%FINALIZE
%--------------------Second part: fusing the frames together------------------------------
%inputs: frameMatrix, has all of the frames
% hop
%outputs: vectorTime:vector from adding frames
sizeMatrix = size(outputy);
% Get number of frames
num_frames = sizeMatrix(1);
% Get size of each frame
size_frames = sizeMatrix(2);
% init
timeIndex = 1;
vectorTime = zeros(num_frames*hopOut-hopOut+size_frames,1);
% Loop for every fram and operlap-add
for index=1:num_frames - 1
vectorTime(timeIndex:timeIndex+size_frames-1) = vectorTime(timeIndex:timeIndex+size_frames-1) + outputy(index,:)';
timeIndex = timeIndex + hopOut;
end
outputVector = vectorTime(1:2:end);
%x(1:2:size(x,1));
return