generated from eduardocorrearaujo/sprint-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDenoiseSVD.m
196 lines (175 loc) · 6.02 KB
/
DenoiseSVD.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
% -----------------------------------------------------------------
% DenoiseSVD.m
% -----------------------------------------------------------------
% Programmer: Americo Cunha Jr
% americo.cunhajr@gmail.com
%
% Originally programmed in: Aug 14, 2024
% Last update in: Sep 06, 2024
% -----------------------------------------------------------------
% This function removes noise from a given time series using
% Singular Value Decomposition (SVD). It constructs a Hankel
% matrix from the time series, performs SVD, and reconstructs
% a denoised time series by truncating small singular values.
%
% Input:
% (double N x 1) x - noisly time series
% (int > 0) w - window length for Hankel matrix
% (int > 0) r - truncation rank (optional)
%
% Output:
% (double N x 1) denoised_x - denoised time series
% (int > 0) r - truncation rank (optional)
% -----------------------------------------------------------------
% Reference:
% D. L. Donoho and M. Gavish
% The Optimal Hard Threshold for Singular Values is 4/sqrt(3),
% IEEE Transactions on Information Theory, 60(8):5040-5053, 2014
% DOI: https://doi.org/10.1109/TIT.2014.2323359
% ArXiv: http://arxiv.org/abs/1305.5870
% -----------------------------------------------------------------
function [x_denoised,r] = DenoiseSVD(x,w,r)
% check number of arguments
if nargin < 2
error('Too few inputs.');
elseif nargin > 3
error('Too many inputs.');
end
% check x for consistency
if ~isvector(x)
error('x must be a column vector.');
elseif size(x,2) ~= 1
% convert to a column vector (if necessary)
x = x(:);
end
% check w for consistency
if ~isscalar(w) || mod(w,1) ~= 0 || w <= 0
error('w must be a positive integer.');
end
% Length of the input time series
N = length(x);
% check that w is not greater than N
if w > N
error('w cannot be greater than the length of the time series.');
end
% Preallocate the Hankel matrix
H = zeros(N-w+1,w);
% Construct the Hankel matrix H from the time series x
for i = 1:N-w+1
H(i,:) = x(i:i+w-1);
end
% Perform Singular Value Decomposition on the Hankel matrix
[U,S,V] = svd(H,'econ');
% Determine the rank r if not provided using Gavish and Donoho's method
if nargin < 3
[m, n] = size(H);
s = diag(S);
beta = min(m, n) / max(m, n);
thresh = optimal_SVHT_coef(beta, 0)*median(s);
r = length(s(s > thresh));
else
% check r for consistency
if ~isscalar(r) || mod(r,1) ~= 0 || r <= 0
error('r must be a positive integer.');
end
% check that r is not greater than w
if r > w
error('r cannot be greater than w.');
end
end
% Truncate the singular values to the top r values
S_r = S;
S_r(r+1:end,r+1:end) = 0;
% Reconstruct the denoised Hankel matrix
H_denoised = U*S_r*V';
% Preallocate the denoised time series and a counter for averaging
x_denoised = zeros(N, 1);
count = zeros(N, 1);
% Reconstruct the denoised time series using diagonal averaging
for i = 1:N-w+1
for j = 1:w
x_denoised(i+j-1) = x_denoised(i+j-1) + H_denoised(i, j);
count(i+j-1) = count(i+j-1) + 1;
end
end
% Normalize by the number of contributions to each element
x_denoised = x_denoised./count;
end
% -----------------------------------------------------------------
% -----------------------------------------------------------------
% This function comoutes the optimal threshold coefficient.
%
% Originally programmed by Donoho and Gavish (2014)
% -----------------------------------------------------------------
function coef = optimal_SVHT_coef(beta,sigma_known)
if sigma_known
coef = optimal_SVHT_coef_sigma_known(beta);
else
coef = optimal_SVHT_coef_sigma_unknown(beta);
end
end
function lambda_star = optimal_SVHT_coef_sigma_known(beta)
assert(all(beta>0));
assert(all(beta<=1));
assert(numel(beta) == length(beta)); % beta must be a vector
w = (8*beta)./(beta+1+sqrt(beta.^2+14*beta+1));
lambda_star = sqrt(2*(beta+1)+w);
end
function omega = optimal_SVHT_coef_sigma_unknown(beta)
assert(all(beta>0));
assert(all(beta<=1));
assert(numel(beta) == length(beta)); % beta must be a vector
coef = optimal_SVHT_coef_sigma_known(beta);
MPmedian = zeros(size(beta));
for i = 1:length(beta)
MPmedian(i) = MedianMarcenkoPastur(beta(i));
end
omega = coef./sqrt(MPmedian);
end
function med = MedianMarcenkoPastur(beta)
MarPas = @(x) 1-incMarPas(x,beta,0);
lobnd = (1-sqrt(beta))^2;
hibnd = (1+sqrt(beta))^2;
change = 1;
while change && (hibnd-lobnd > 0.001)
change = 0;
x = linspace(lobnd,hibnd,5);
y = arrayfun(MarPas,x);
if any(y < 0.5)
lobnd = max(x(y < 0.5));
change = 1;
end
if any(y > 0.5)
hibnd = min(x(y > 0.5));
change = 1;
end
end
med = (hibnd+lobnd)/2;
end
function I = incMarPas(x0, beta, gamma)
if beta > 1
error('beta beyond valid range.');
end
topSpec = (1+sqrt(beta))^2;
botSpec = (1-sqrt(beta))^2;
Q = @(x) (topSpec-x).*(x-botSpec) > 0;
point = @(x) sqrt((topSpec-x).*(x-botSpec))./(beta.*x)/(2*pi);
counterPoint = 0;
MarPas = @(x) IfElse(Q(x),point(x),counterPoint);
if gamma ~= 0
fun = @(x) (x.^gamma.*MarPas(x));
else
fun = @(x) MarPas(x);
end
I = integral(fun,x0,topSpec);
end
function y = IfElse(Q, point, counterPoint)
y = point;
if any(~Q)
if length(counterPoint) == 1
counterPoint = ones(size(Q)) .* counterPoint;
end
y(~Q) = counterPoint(~Q);
end
end
% -----------------------------------------------------------------