Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
compressed_sensing
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Erik Strand
compressed_sensing
Commits
1c70365e
Commit
1c70365e
authored
6 years ago
by
Erik Strand
Browse files
Options
Downloads
Patches
Plain Diff
Perform the DCT in C++
parent
4eab096c
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
main.cpp
+36
-5
36 additions, 5 deletions
main.cpp
plotter.py
+8
-2
8 additions, 2 deletions
plotter.py
with
44 additions
and
7 deletions
main.cpp
+
36
−
5
View file @
1c70365e
#include
<Eigen/Core>
#include
<iostream>
//--------------------------------------------------------------------------------------------------
using
Scalar
=
double
;
using
Vector
=
Eigen
::
Matrix
<
Scalar
,
Eigen
::
Dynamic
,
1
>
;
using
Matrix
=
Eigen
::
Matrix
<
Scalar
,
Eigen
::
Dynamic
,
Eigen
::
Dynamic
>
;
//--------------------------------------------------------------------------------------------------
constexpr
Scalar
pi
=
3.1415926535897932384626433832795
;
//--------------------------------------------------------------------------------------------------
void
python_print
(
char
const
*
name
,
Vector
const
&
x
)
{
std
::
cout
<<
name
<<
" = np.array(["
;
std
::
cout
<<
x
[
0
];
...
...
@@ -14,23 +19,49 @@ void python_print(char const* name, Vector const& x) {
std
::
cout
<<
"])
\n
"
;
}
//--------------------------------------------------------------------------------------------------
Vector
sample_two_sins
(
Scalar
f1
,
Scalar
f2
,
Vector
const
&
sample_times
)
{
Vector
const
sample_rads
=
2
*
pi
*
sample_times
;
return
sin
((
f1
*
sample_rads
).
array
())
+
sin
((
f2
*
sample_rads
).
array
());
}
//--------------------------------------------------------------------------------------------------
Matrix
compute_dct_matrix
(
uint32_t
n_samples
)
{
Matrix
dct_matrix
(
n_samples
,
n_samples
);
Scalar
root_one_over_n
=
std
::
sqrt
(
1.0
/
n_samples
);
Scalar
root_two_over_n
=
std
::
sqrt
(
2.0
/
n_samples
);
for
(
uint32_t
j
=
0
;
j
<
n_samples
;
++
j
)
{
dct_matrix
(
0
,
j
)
=
root_one_over_n
;
}
for
(
uint32_t
i
=
1
;
i
<
n_samples
;
++
i
)
{
for
(
uint32_t
j
=
0
;
j
<
n_samples
;
++
j
)
{
dct_matrix
(
i
,
j
)
=
root_two_over_n
*
cos
(
pi
*
(
2
*
j
+
1
)
*
i
/
(
2
*
n_samples
));
}
}
return
dct_matrix
;
}
//--------------------------------------------------------------------------------------------------
int
main
()
{
//Vector x = Vector::Random(5);
//std::cout << x << '\n';
Scalar
pi
=
3.1415926535897932384626433832795
;
Scalar
f1
=
697
;
Scalar
f2
=
1209
;
// Part (a)
Scalar
sample_period
=
0.01
;
uint32_t
n_samples
=
10
;
Vector
sample_times
=
(
sample_period
/
n_samples
)
*
Vector
::
LinSpaced
(
n_samples
,
0
,
n_samples
);
uint32_t
n_samples
=
250
;
Vector
const
sample_times
=
(
sample_period
/
n_samples
)
*
Vector
::
LinSpaced
(
n_samples
,
0
,
n_samples
);
Vector
const
sample_values
=
sample_two_sins
(
f1
,
f2
,
sample_times
);
python_print
(
"sample_times"
,
sample_times
);
Vector
sample_rads
=
2
*
pi
*
sample_times
;
Vector
sample_values
=
sin
((
f1
*
sample_rads
).
array
())
+
sin
((
f2
*
sample_rads
).
array
());
python_print
(
"sample_values"
,
sample_values
);
// Part (b)
Matrix
dct_matrix
=
compute_dct_matrix
(
n_samples
);
Vector
dct
=
dct_matrix
*
sample_values
;
python_print
(
"dct"
,
dct
);
return
0
;
}
This diff is collapsed.
Click to expand it.
plotter.py
+
8
−
2
View file @
1c70365e
import
numpy
as
np
import
matplotlib.pyplot
as
plt
sample_times
=
np
.
array
([
0
,
4.01606e-05
,
8.03213e-05
,
0.000120482
,
0.000160643
,
0.000200803
,
0.000240964
,
0.000281124
,
0.000321285
,
0.000361446
,
0.000401606
,
0.000441767
,
0.000481928
,
0.000522088
,
0.000562249
,
0.00060241
,
0.00064257
,
0.000682731
,
0.000722892
,
0.000763052
,
0.000803213
,
0.000843373
,
0.000883534
,
0.000923695
,
0.000963855
,
0.00100402
,
0.00104418
,
0.00108434
,
0.0011245
,
0.00116466
,
0.00120482
,
0.00124498
,
0.00128514
,
0.0013253
,
0.00136546
,
0.00140562
,
0.00144578
,
0.00148594
,
0.0015261
,
0.00156627
,
0.00160643
,
0.00164659
,
0.00168675
,
0.00172691
,
0.00176707
,
0.00180723
,
0.00184739
,
0.00188755
,
0.00192771
,
0.00196787
,
0.00200803
,
0.00204819
,
0.00208835
,
0.00212851
,
0.00216867
,
0.00220884
,
0.002249
,
0.00228916
,
0.00232932
,
0.00236948
,
0.00240964
,
0.0024498
,
0.00248996
,
0.00253012
,
0.00257028
,
0.00261044
,
0.0026506
,
0.00269076
,
0.00273092
,
0.00277108
,
0.00281124
,
0.00285141
,
0.00289157
,
0.00293173
,
0.00297189
,
0.00301205
,
0.00305221
,
0.00309237
,
0.00313253
,
0.00317269
,
0.00321285
,
0.00325301
,
0.00329317
,
0.00333333
,
0.00337349
,
0.00341365
,
0.00345382
,
0.00349398
,
0.00353414
,
0.0035743
,
0.00361446
,
0.00365462
,
0.00369478
,
0.00373494
,
0.0037751
,
0.00381526
,
0.00385542
,
0.00389558
,
0.00393574
,
0.0039759
,
0.00401606
,
0.00405622
,
0.00409639
,
0.00413655
,
0.00417671
,
0.00421687
,
0.00425703
,
0.00429719
,
0.00433735
,
0.00437751
,
0.00441767
,
0.00445783
,
0.00449799
,
0.00453815
,
0.00457831
,
0.00461847
,
0.00465863
,
0.0046988
,
0.00473896
,
0.00477912
,
0.00481928
,
0.00485944
,
0.0048996
,
0.00493976
,
0.00497992
,
0.00502008
,
0.00506024
,
0.0051004
,
0.00514056
,
0.00518072
,
0.00522088
,
0.00526104
,
0.0053012
,
0.00534137
,
0.00538153
,
0.00542169
,
0.00546185
,
0.00550201
,
0.00554217
,
0.00558233
,
0.00562249
,
0.00566265
,
0.00570281
,
0.00574297
,
0.00578313
,
0.00582329
,
0.00586345
,
0.00590361
,
0.00594378
,
0.00598394
,
0.0060241
,
0.00606426
,
0.00610442
,
0.00614458
,
0.00618474
,
0.0062249
,
0.00626506
,
0.00630522
,
0.00634538
,
0.00638554
,
0.0064257
,
0.00646586
,
0.00650602
,
0.00654618
,
0.00658635
,
0.00662651
,
0.00666667
,
0.00670683
,
0.00674699
,
0.00678715
,
0.00682731
,
0.00686747
,
0.00690763
,
0.00694779
,
0.00698795
,
0.00702811
,
0.00706827
,
0.00710843
,
0.00714859
,
0.00718876
,
0.00722892
,
0.00726908
,
0.00730924
,
0.0073494
,
0.00738956
,
0.00742972
,
0.00746988
,
0.00751004
,
0.0075502
,
0.00759036
,
0.00763052
,
0.00767068
,
0.00771084
,
0.007751
,
0.00779116
,
0.00783133
,
0.00787149
,
0.00791165
,
0.00795181
,
0.00799197
,
0.00803213
,
0.00807229
,
0.00811245
,
0.00815261
,
0.00819277
,
0.00823293
,
0.00827309
,
0.00831325
,
0.00835341
,
0.00839357
,
0.00843373
,
0.0084739
,
0.00851406
,
0.00855422
,
0.00859438
,
0.00863454
,
0.0086747
,
0.00871486
,
0.00875502
,
0.00879518
,
0.00883534
,
0.0088755
,
0.00891566
,
0.00895582
,
0.00899598
,
0.00903614
,
0.00907631
,
0.00911647
,
0.00915663
,
0.00919679
,
0.00923695
,
0.00927711
,
0.00931727
,
0.00935743
,
0.00939759
,
0.00943775
,
0.00947791
,
0.00951807
,
0.00955823
,
0.00959839
,
0.00963855
,
0.00967871
,
0.00971888
,
0.00975904
,
0.0097992
,
0.00983936
,
0.00987952
,
0.00991968
,
0.00995984
,
0.01
])
sample_values
=
np
.
array
([
0
,
0.475338
,
0.917539
,
1.29619
,
1.5861
,
1.76932
,
1.83651
,
1.78761
,
1.6316
,
1.38558
,
1.0731
,
0.721928
,
0.361558
,
0.0205522
,
-
0.27594
,
-
0.508314
,
-
0.664077
,
-
0.738727
,
-
0.735864
,
-
0.666533
,
-
0.547871
,
-
0.401193
,
-
0.249706
,
-
0.116065
,
-
0.0200166
,
0.0236569
,
0.00668195
,
-
0.0718098
,
-
0.205127
,
-
0.379618
,
-
0.575975
,
-
0.771114
,
-
0.94046
,
-
1.06042
,
-
1.1108
,
-
1.07697
,
-
0.951487
,
-
0.73516
,
-
0.43726
,
-
0.0750177
,
0.327653
,
0.742071
,
1.13718
,
1.48217
,
1.7492
,
1.91577
,
1.96681
,
1.89599
,
1.70642
,
1.41042
,
1.02857
,
0.588008
,
0.120163
,
-
0.341839
,
-
0.765935
,
-
1.12376
,
-
1.39294
,
-
1.5588
,
-
1.61544
,
-
1.56598
,
-
1.42199
,
-
1.20225
,
-
0.930797
,
-
0.634626
,
-
0.341124
,
-
0.0755387
,
0.141293
,
0.294754
,
0.377636
,
0.390584
,
0.341783
,
0.245913
,
0.122481
,
-
0.00631234
,
-
0.117955
,
-
0.19203
,
-
0.212412
,
-
0.169044
,
-
0.0591181
,
0.112454
,
0.333336
,
0.584794
,
0.843377
,
1.08309
,
1.27787
,
1.40408
,
1.44291
,
1.38232
,
1.21838
,
0.95598
,
0.608661
,
0.197701
,
-
0.249515
,
-
0.701696
,
-
1.12623
,
-
1.49192
,
-
1.77159
,
-
1.94442
,
-
1.99773
,
-
1.92798
,
-
1.74113
,
-
1.45207
,
-
1.08332
,
-
0.663067
,
-
0.222778
,
0.205515
,
0.591886
,
0.910992
,
1.14409
,
1.28044
,
1.31795
,
1.26307
,
1.12989
,
0.938508
,
0.713012
,
0.478999
,
0.261091
,
0.0805585
,
-
0.0466774
,
-
0.11149
,
-
0.112311
,
-
0.0551393
,
0.0472029
,
0.176484
,
0.311019
,
0.427974
,
0.505792
,
0.526522
,
0.477802
,
0.354329
,
0.158647
,
-
0.0988122
,
-
0.400459
,
-
0.722977
,
-
1.03934
,
-
1.32121
,
-
1.54155
,
-
1.67714
,
-
1.71079
,
-
1.63312
,
-
1.44356
,
-
1.15069
,
-
0.771708
,
-
0.33113
,
0.14113
,
0.6122
,
1.04895
,
1.42074
,
1.70194
,
1.87404
,
1.9271
,
1.86053
,
1.68299
,
1.41149
,
1.06984
,
0.686454
,
0.291854
,
-
0.0840174
,
-
0.414358
,
-
0.677735
,
-
0.859784
,
-
0.954231
,
-
0.963144
,
-
0.896411
,
-
0.770496
,
-
0.606599
,
-
0.428405
,
-
0.259632
,
-
0.121617
,
-
0.0311657
,
0.00112032
,
-
0.0281042
,
-
0.11461
,
-
0.247029
,
-
0.408011
,
-
0.575983
,
-
0.727348
,
-
0.838913
,
-
0.890317
,
-
0.866221
,
-
0.75805
,
-
0.565119
,
-
0.295029
,
0.0367266
,
0.407896
,
0.791266
,
1.15696
,
1.47502
,
1.71807
,
1.86376
,
1.89685
,
1.81066
,
1.60779
,
1.30007
,
0.907671
,
0.457475
,
-
0.019091
,
-
0.488655
,
-
0.918654
,
-
1.28001
,
-
1.54948
,
-
1.71149
,
-
1.75931
,
-
1.69539
,
-
1.53092
,
-
1.28466
,
-
0.981105
,
-
0.648113
,
-
0.314394
,
-
0.00689592
,
0.25156
,
0.444213
,
0.561694
,
0.602629
,
0.573475
,
0.487607
,
0.363754
,
0.223933
,
0.0910946
,
-
0.0133094
,
-
0.0715908
,
-
0.0717057
,
-
0.00857246
,
0.115302
,
0.289952
,
0.498828
,
0.720336
,
0.929905
,
1.10238
,
1.21454
,
1.24743
,
1.18842
,
1.03264
,
0.783761
,
0.453978
,
0.0632207
,
-
0.36236
,
-
0.792506
,
-
1.19536
,
-
1.54017
,
-
1.79994
,
-
1.95375
,
-
1.9886
,
-
1.9006
,
-
1.69531
,
-
1.38735
,
-
0.999124
,
-
0.559002
,
-
0.0988865
,
0.348445
])
dct
=
np
.
array
([
0.0493937
,
1.53249
,
0.0703373
,
1.58311
,
0.071832
,
1.69718
,
0.0744769
,
1.91051
,
0.0785496
,
2.32168
,
0.0845789
,
3.29439
,
0.0938324
,
8.09608
,
-
0.948385
,
-
5.98402
,
0.120253
,
-
1.12508
,
0.152546
,
0.119667
,
0.214371
,
1.22218
,
0.385667
,
4.39185
,
3.03895
,
-
8.90656
,
-
0.469319
,
-
2.524
,
-
0.208824
,
-
1.50006
,
-
0.13078
,
-
1.06566
,
-
0.0934123
,
-
0.821045
,
-
0.0716006
,
-
0.662819
,
-
0.0573687
,
-
0.551709
,
-
0.0473917
,
-
0.469331
,
-
0.0400375
,
-
0.405854
,
-
0.0344114
,
-
0.355506
,
-
0.0299824
,
-
0.314665
,
-
0.0264156
,
-
0.280932
,
-
0.0234894
,
-
0.252653
,
-
0.0210515
,
-
0.228649
,
-
0.0189938
,
-
0.208056
,
-
0.0172374
,
-
0.190227
,
-
0.0157237
,
-
0.174667
,
-
0.0144081
,
-
0.16099
,
-
0.013256
,
-
0.148893
,
-
0.0122403
,
-
0.138131
,
-
0.0113395
,
-
0.128509
,
-
0.0105363
,
-
0.119865
,
-
0.00981652
,
-
0.112068
,
-
0.00916866
,
-
0.105006
,
-
0.00858312
,
-
0.0985874
,
-
0.00805189
,
-
0.092734
,
-
0.00756824
,
-
0.0873795
,
-
0.0071265
,
-
0.0824671
,
-
0.00672179
,
-
0.0779483
,
-
0.00634998
,
-
0.073781
,
-
0.00600749
,
-
0.0699287
,
-
0.00569123
,
-
0.0663596
,
-
0.0053985
,
-
0.0630459
,
-
0.00512696
,
-
0.0599632
,
-
0.00487456
,
-
0.0570899
,
-
0.00463947
,
-
0.0544069
,
-
0.00442012
,
-
0.0518974
,
-
0.00421508
,
-
0.0495463
,
-
0.00402309
,
-
0.0473402
,
-
0.00384303
,
-
0.045267
,
-
0.00367391
,
-
0.0433158
,
-
0.00351482
,
-
0.0414771
,
-
0.00336496
,
-
0.0397421
,
-
0.0032236
,
-
0.0381027
,
-
0.00309008
,
-
0.0365518
,
-
0.00296382
,
-
0.035083
,
-
0.00284426
,
-
0.0336903
,
-
0.00273093
,
-
0.0323682
,
-
0.00262338
,
-
0.0311119
,
-
0.0025212
,
-
0.0299168
,
-
0.00242403
,
-
0.0287789
,
-
0.00233151
,
-
0.0276942
,
-
0.00224334
,
-
0.0266594
,
-
0.00215923
,
-
0.0256711
,
-
0.00207892
,
-
0.0247266
,
-
0.00200217
,
-
0.0238229
,
-
0.00192875
,
-
0.0229577
,
-
0.00185846
,
-
0.0221285
,
-
0.0017911
,
-
0.0213332
,
-
0.0017265
,
-
0.0205698
,
-
0.00166449
,
-
0.0198364
,
-
0.00160493
,
-
0.0191312
,
-
0.00154766
,
-
0.0184527
,
-
0.00149255
,
-
0.0177994
,
-
0.00143949
,
-
0.0171698
,
-
0.00138835
,
-
0.0165626
,
-
0.00133904
,
-
0.0159765
,
-
0.00129144
,
-
0.0154105
,
-
0.00124547
,
-
0.0148635
,
-
0.00120103
,
-
0.0143343
,
-
0.00115805
,
-
0.0138221
,
-
0.00111644
,
-
0.013326
,
-
0.00107613
,
-
0.0128451
,
-
0.00103706
,
-
0.0123786
,
-
0.000999157
,
-
0.0119258
,
-
0.00096236
,
-
0.011486
,
-
0.000926612
,
-
0.0110584
,
-
0.000891859
,
-
0.0106425
,
-
0.000858049
,
-
0.0102376
,
-
0.000825132
,
-
0.00984319
,
-
0.000793062
,
-
0.00945872
,
-
0.000761795
,
-
0.00908367
,
-
0.00073129
,
-
0.00871754
,
-
0.000701505
,
-
0.00835987
,
-
0.000672404
,
-
0.00801022
,
-
0.000643949
,
-
0.00766816
,
-
0.000616107
,
-
0.00733328
,
-
0.000588845
,
-
0.0070052
,
-
0.00056213
,
-
0.00668355
,
-
0.000535933
,
-
0.00636796
,
-
0.000510224
,
-
0.0060581
,
-
0.000484977
,
-
0.00575364
,
-
0.000460163
,
-
0.00545426
,
-
0.000435757
,
-
0.00515967
,
-
0.000411735
,
-
0.00486955
,
-
0.000388072
,
-
0.00458364
,
-
0.000364745
,
-
0.00430165
,
-
0.000341732
,
-
0.00402332
,
-
0.000319012
,
-
0.00374839
,
-
0.000296562
,
-
0.00347661
,
-
0.000274362
,
-
0.00320774
,
-
0.000252393
,
-
0.00294153
,
-
0.000230635
,
-
0.00267776
,
-
0.000209069
,
-
0.00241619
,
-
0.000187676
,
-
0.0021566
,
-
0.000166438
,
-
0.00189878
,
-
0.000145337
,
-
0.00164251
,
-
0.000124355
,
-
0.00138757
,
-
0.000103475
,
-
0.00113376
,
-
8.26789e-05
,
-
0.000880865
,
-
6.19504e-05
,
-
0.000628682
,
-
4.12723e-05
,
-
0.000377007
,
-
2.06278e-05
,
-
0.000125635
])
if
__name__
==
"
__main__
"
:
sample_times
=
np
.
array
([
0
,
0.00111111
,
0.00222222
,
0.00333333
,
0.00444444
,
0.00555556
,
0.00666667
,
0.00777778
,
0.00888889
,
0.01
])
sample_values
=
np
.
array
([
0
,
-
0.155307
,
-
1.22423
,
1.08309
,
1.2909
,
-
1.69749
,
-
0.428405
,
1.04634
,
-
0.0577232
,
0.348445
])
plt
.
plot
(
sample_times
,
sample_values
)
plt
.
savefig
(
"
fig_a.png
"
)
plt
.
close
()
plt
.
plot
(
np
.
arange
(
len
(
dct
)),
dct
)
plt
.
savefig
(
"
fig_b.png
"
)
plt
.
close
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment