-
Notifications
You must be signed in to change notification settings - Fork 6
/
test_linear_transforms.m
309 lines (246 loc) · 15.2 KB
/
test_linear_transforms.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
%
% test classes for linear transform
%
% author: Martin F. Schiffner
% date: 2016-08-13
% modified: 2020-04-03
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% clear workspace
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
close all;
clear;
clc;
addpath( genpath( sprintf( '/opt/matlab/R2013b/toolbox/WaveAtom-1.1.1/' ) ) );
addpath( genpath( sprintf( '/opt/matlab/R2013b/toolbox/Wavelab850/' ) ) );
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% independent
N_dimensions = 2;
scale_finest = 9;
scale_coarsest = 2;
N_cases = 25;
% dependent
N_points_axis = repmat( 2.^scale_finest, [ 1, N_dimensions ] );
N_points = prod( N_points_axis );
weights = randn( N_points, 1 ) + 1j * randn( N_points, 1 );
%--------------------------------------------------------------------------
% define transforms
%--------------------------------------------------------------------------
% orthonormal transforms
LT_identity = linear_transforms.identity( N_points );
LT_fourier = linear_transforms.fourier( N_points_axis );
% LT_fourier_blk = linear_transforms.fourier_block( N_points_axis, [32, 32] );
% wavelets
LT_wavelet_battle = linear_transforms.wavelet( linear_transforms.wavelets.battle( 5 ), N_dimensions, scale_finest, scale_coarsest );
LT_wavelet_beylkin = linear_transforms.wavelet( linear_transforms.wavelets.beylkin, N_dimensions, scale_finest, scale_coarsest );
LT_wavelet_coiflet = linear_transforms.wavelet( linear_transforms.wavelets.coiflet( 5 ), N_dimensions, scale_finest, scale_coarsest );
LT_wavelet_db4 = linear_transforms.wavelet( linear_transforms.wavelets.daubechies( 4 ), N_dimensions, scale_finest, scale_coarsest );
LT_wavelet_db10 = linear_transforms.wavelet( linear_transforms.wavelets.daubechies( 10 ), N_dimensions, scale_finest, scale_coarsest );
LT_wavelet_db20 = linear_transforms.wavelet( linear_transforms.wavelets.daubechies( 20 ), N_dimensions, scale_finest, scale_coarsest );
LT_wavelet_haar = linear_transforms.wavelet( linear_transforms.wavelets.haar, N_dimensions, scale_finest, scale_coarsest );
LT_wavelet_symmlet = linear_transforms.wavelet( linear_transforms.wavelets.symmlet( 10 ), N_dimensions, scale_finest, scale_coarsest );
LT_wavelet_vaidyanathan = linear_transforms.wavelet( linear_transforms.wavelets.vaidyanathan, N_dimensions, scale_finest, scale_coarsest );
LT_wavelets = { LT_wavelet_battle; LT_wavelet_beylkin; LT_wavelet_coiflet; LT_wavelet_db4; LT_wavelet_db10; LT_wavelet_db20; LT_wavelet_haar; LT_wavelet_symmlet; LT_wavelet_vaidyanathan };
% wave atoms
%
options.qmf = MakeONFilter( 'Daubechies', 4 );
options.scale_coarsest = 0;
op_psi = @(x, mode) psi_wavelet( N_points_axis, x, mode, options );
% invertible transforms
LT_weighting_1 = linear_transforms.weighting( weights );
% linear transforms
LTs_wave_atom = linear_transforms.wave_atom( N_points_axis, [ "ortho", "directional", "complex" ] );
LT_wave_atom_ortho = LTs_wave_atom( 1 );
LT_wave_atom_directional = LTs_wave_atom( 2 );
LT_wave_atom_complex = LTs_wave_atom( 3 );
LT_curvelet = linear_transforms.curvelet( N_points_axis );
% concatenated transforms
LT_concatenate_vertical = linear_transforms.concatenations.vertical( LT_identity, LT_weighting_1, LT_fourier, LT_wavelet_db10 );
LT_concatenate_diagonal = linear_transforms.concatenations.diagonal( LT_identity, LT_weighting_1, LT_fourier, LT_wavelet_db10 );
% composite transforms
size_transform = LT_concatenate_vertical.size_transform;
N_weights_2 = size_transform(1);
weights_2 = ones( N_weights_2, 1 ) + 1j * ones( N_weights_2, 1 );
LT_weighting_2 = linear_transforms.weighting( weights_2 );
LT_composite = linear_transforms.composition( LT_weighting_2, LT_concatenate_vertical, LT_weighting_1 );
%--------------------------------------------------------------------------
% setup test
%--------------------------------------------------------------------------
% transforms = { LT_identity, LT_fourier, LT_fourier_block, LT_wavelet_haar, LT_wavelet_db4, LT_wavelet_db10, LT_wavelet_db20, LT_weighting_1, LT_wave_atom_ortho, LT_wave_atom_directional, LT_wave_atom_complex, LT_curvelet, LT_concatenate_vertical, LT_concatenate_diagonal, LT_composite };
transforms = { LT_identity, LT_fourier, LT_wavelets{ : } };
N_transforms = numel( transforms );
error_fwd_inv = zeros( N_transforms, N_cases );
error_inv_fwd = zeros( N_transforms, N_cases );
error_adj = zeros( N_transforms, N_cases );
norms_cols = zeros( N_transforms, N_cases );
for index_transform = 1:N_transforms
% print status information
fprintf( 'index_transform = %d of %d (%s):', index_transform, N_transforms, class( transforms{ index_transform } ) );
% get size of transform
size_transform = operator_transform( transforms{ index_transform }, [], 0 );
N_coefficients_act = size_transform( 1 );
N_points_act = size_transform( 2 );
indices_rand = randperm( N_coefficients_act );
% iterate over random cases
for index_case = 1:N_cases
% print case index
fprintf( ' %d', index_case );
%------------------------------------------------------------------
% a) forward and inverse transforms
%------------------------------------------------------------------
% generate random test vector
test_lat = randn( N_points_act, 1 ) + 1j * randn( N_points_act, 1 );
test_lat_norm = norm( test_lat( : ), 2 );
% compute forward transform
test_lat_fwd = forward_transform( transforms{ index_transform }, test_lat );
% test_lat_fwd_old = op_psi( test_lat, 1 );
if isa( transforms{ index_transform }, 'linear_transforms.attributes.invertible' )
% compute inverse transform
test_lat_inv = inverse_transform( transforms{ index_transform }, test_lat_fwd );
% test_lat_inv_old = op_psi( test_lat_fwd, 2 );
% compute rel. RMSEs
error_fwd_inv( index_transform, index_case ) = norm( test_lat_inv( : ) - test_lat( : ) ) / test_lat_norm;
end % if isa( transforms{ index_transform }, 'linear_transforms.attributes.invertible' )
%------------------------------------------------------------------
% b) adjoint and inverse transforms
%------------------------------------------------------------------
% generate random test vector
test_coef = randn( N_coefficients_act, 1 ) + 1j * randn( N_coefficients_act, 1 );
test_coef_norm = norm( test_coef( : ), 2 );
% compute adjoint transform
test_coef_adj = adjoint_transform( transforms{ index_transform }, test_coef );
if isa( transforms{ index_transform }, 'linear_transforms.attributes.invertible' )
% compute inverse transform
test_coef_inv = inverse_transform( transforms{ index_transform }, test_coef );
% compute forward transform
test_coef_fwd = operator_transform( transforms{ index_transform }, test_coef_inv, 1 );
% compute rel. RMSEs
error_inv_fwd( index_transform, index_case ) = norm( test_coef_fwd( : ) - test_coef( : ) ) / test_coef_norm;
end % if isa( transforms{ index_transform }, 'linear_transforms.attributes.invertible' )
%------------------------------------------------------------------
% c) adjointness test
%------------------------------------------------------------------
error_adj( index_transform, index_case ) = test_lat_fwd.' * conj( test_coef( : ) ) - test_lat.' * conj( test_coef_adj( : ) );
%------------------------------------------------------------------
% d) column norms
%------------------------------------------------------------------
test_coef = zeros( N_coefficients_act, 1 );
test_coef( indices_rand( index_case ) ) = 1;
% compute adjoint transform
test_coef_adj = adjoint_transform( transforms{ index_transform }, test_coef );
% compute norm
norms_cols( index_transform, index_case ) = norm( test_coef_adj( : ) );
end % for index_case = 1:N_cases
% print status
fprintf( '\n');
end % for index_transform = 1:N_transforms
% statistics of results
error_fwd_inv_mean = mean( error_fwd_inv, 2 ) * 1e2;
error_adj_fwd_mean = mean( error_inv_fwd, 2 ) * 1e2;
error_adj_mean = mean( error_adj, 2 );
norms_cols_mean = mean( norms_cols, 2 );
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% test convolution
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%--------------------------------------------------------------------------
% 0.) parameters
%--------------------------------------------------------------------------
N_signals = 1;
T_s = physical_values.second( 1 / 40e6 );
N_samples = 3200;
N_samples_shift = 78;
axis_t = math.sequence_increasing_regular_quantized( N_samples_shift, N_samples_shift + N_samples - 1, T_s );
f_0 = physical_values.hertz( 4e6 );
interval_f = math.interval( physical_values.hertz( 2.25e6 ), physical_values.hertz( 6.75e6 ) );
%--------------------------------------------------------------------------
% 1.) create bandpass signal
%--------------------------------------------------------------------------
samples_BP_tilde = sin( 2 * pi * f_0 * ( axis_t.members - N_samples_shift * T_s ) );
signal_BP_tilde = processing.signal_matrix( axis_t, repmat( samples_BP_tilde, [ 1, N_signals ] ) );
%--------------------------------------------------------------------------
% 2.) create time-dependent variable gain
%--------------------------------------------------------------------------
TGC_curve = regularization.tgc.curves.exponential( math.interval_quantized( axis_t.q_lb, axis_t.q_ub + 1, axis_t.delta ), physical_values.hertz( 6e4 ) );
% sample TGC curve
samples_gain_tilde = sample_curve( TGC_curve, axis_t );
signal_gain_tilde = processing.signal( axis_t, samples_gain_tilde );
%--------------------------------------------------------------------------
% 3.) apply TGC in the time domain
%--------------------------------------------------------------------------
signal_BP_tgc_tilde = signal_BP_tilde .* signal_gain_tilde;
signal_BP_tgc = fourier_coefficients( signal_BP_tgc_tilde, [], interval_f );
signal_BP_tgc_tilde_bp = signal( signal_BP_tgc, N_samples_shift, T_s );
%--------------------------------------------------------------------------
% 4.) apply TGC in the frequency domain (convolution)
%--------------------------------------------------------------------------
% Fourier coefficients (numeric)
signal_BP = fourier_coefficients( signal_BP_tilde, [], interval_f );
signal_gain = fourier_coefficients( signal_gain_tilde, [], interval_f );
% Fourier coefficients (analytic)
TGC_curve_coef = fourier_coefficients( TGC_curve, abs( axis_t ) * T_s, -50 );
% error in gain Fourier coefficients
% rel_RMSE_coef = norm( signal_gain.samples - TGC_curve_coef.samples ) / norm( TGC_curve_coef.samples );
% define convolution
kernel = [ conj( TGC_curve_coef.samples( end:-1:2 ) ); TGC_curve_coef.samples ];
kernel_analy = [ TGC_curve_coef.samples( 1 ); 2 * TGC_curve_coef.samples( 2:end ) ];
LT_convolution_dft = linear_transforms.convolutions.fft( kernel, abs( signal_BP.axis ) );
LT_convolution_mat = linear_transforms.convolutions.matrix( kernel, abs( signal_BP.axis ) );
% apply convolution
samples_BP_tgc_conv_dft = forward_transform( LT_convolution_dft, signal_BP.samples );
samples_BP_tgc_conv_mat = forward_transform( LT_convolution_mat, signal_BP.samples );
% apply adjoint convolution
samples_BP_tgc_conv_adj_dft = adjoint_transform( LT_convolution_dft, samples_BP_tgc_conv_dft );
samples_BP_tgc_conv_adj_mat = adjoint_transform( LT_convolution_mat, samples_BP_tgc_conv_dft );
% errors
rel_RMSE_fwd = norm( samples_BP_tgc_conv_dft( : ) - samples_BP_tgc_conv_mat( : ) ) / norm( samples_BP_tgc_conv_mat( : ) );
rel_RMSE_adj = norm( samples_BP_tgc_conv_adj_dft( : ) - samples_BP_tgc_conv_adj_mat( : ) ) / norm( samples_BP_tgc_conv_adj_mat( : ) );
% create signal matrix
signal_BP_tgc_conv_dft = processing.signal_matrix( signal_BP.axis, samples_BP_tgc_conv_dft );
signal_BP_tgc_conv_mat = processing.signal_matrix( signal_BP.axis, samples_BP_tgc_conv_mat );
% time-domain signals
signal_BP_tgc_conv_dft_tilde = signal( signal_BP_tgc_conv_dft, N_samples_shift, T_s );
signal_BP_tgc_conv_mat_tilde = signal( signal_BP_tgc_conv_mat, N_samples_shift, T_s );
% relative RMSEs
rel_RMSE_dft = norm( signal_BP_tgc_conv_dft_tilde.samples - signal_BP_tgc_tilde_bp.samples ) / norm( signal_BP_tgc_tilde_bp.samples );
rel_RMSE_mat = norm( signal_BP_tgc_conv_mat_tilde.samples - signal_BP_tgc_tilde_bp.samples ) / norm( signal_BP_tgc_tilde_bp.samples );
% convolution is similar to overlap add: individual convolutions + overlap
% M_kernel = ( numel( kernel ) - 1 ) / 2;
%
% test_result = conv( signal_BP.samples, kernel );
% q_lb_conv = signal_BP.axis.q_lb - M_kernel;
% q_ub_conv = signal_BP.axis.q_ub + M_kernel;
% index_overlap_start = 1;
% index_overlap_stop = - 2 * signal_BP.axis.q_lb + M_kernel + 1;
%
% % create conjugate even result -> corresponds to real part of analytic signal
% test_result = test_result( ( M_kernel + 1 ):( end - M_kernel ) ) + [ conj( test_result( index_overlap_stop:-1:index_overlap_start ) ); zeros( numel( signal_BP.samples ) - index_overlap_stop, 1 ) ];
% test_result = processing.signal_matrix( signal_BP.axis, test_result );
% test_result_tilde = signal( test_result, N_samples_shift, T_s );
%
% test_result_analy = conv( signal_BP.samples, kernel_analy );
% test_result_analy = test_result_analy(1:end - numel( kernel_analy ) + 1);
% test_result_analy = processing.signal_matrix( signal_BP.axis, test_result_analy );
% test_result_analy_tilde = signal( test_result_analy, N_samples_shift, T_s );
%
% temp = [ conj( signal_BP.samples( end:-1:2 ) ); signal_BP.samples ];
% temp_result = conv( temp, kernel );
% temp_result = temp_result( ( M_kernel + 1 ):( end - M_kernel ) );
% temp_result = temp_result( ( numel( signal_BP.samples ) ):end );
% temp_result = processing.signal_matrix( signal_BP.axis, temp_result );
% temp_result_tilde = signal( temp_result, N_samples_shift, T_s );
% rel_RMSE_temp = norm( temp_result_tilde.samples - signal_BP_tgc_tilde.samples ) / norm( signal_BP_tgc_tilde.samples );
% TGC_curve_coef.
figure( 1 );
plot( double( signal_BP_tgc_conv_dft_tilde.axis.members ), signal_BP_tgc_conv_dft_tilde.samples, ...
double( signal_BP_tgc_conv_mat_tilde.axis.members ), signal_BP_tgc_conv_mat_tilde.samples, ...
signal_BP_tgc_tilde_bp.axis.members, signal_BP_tgc_tilde_bp.samples );
title( sprintf( 'rel. RMSE = %.2f %%', rel_RMSE_dft * 1e2 ) );
%--------------------------------------------------------------------------
% b) test for adjointness
%--------------------------------------------------------------------------
% specify test vectors
test_in = randn( LT_convolution_dft.N_points, 1 ) + 1j * randn( LT_convolution_dft.N_points, 1 );
test_out = randn( LT_convolution_dft.N_coefficients, 1 ) + 1j * randn( LT_convolution_dft.N_coefficients, 1 );
% adjointness must be close to zero
adjointness = ( test_out' * forward_transform( LT_convolution_dft, test_in ) - adjoint_transform( LT_convolution_dft, test_out )' * test_in ) / ( test_out' * forward_transform( LT_convolution_dft, test_in ) );