Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
aerobs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Andreas Gattringer
aerobs
Commits
ffcdeecf
Commit
ffcdeecf
authored
9 months ago
by
Andreas Gattringer
Browse files
Options
Downloads
Patches
Plain Diff
dmatest: vary sampling frequency based on signal processing
parent
dfb05c30
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
programs/dmatest_v2.py
+29
-7
29 additions, 7 deletions
programs/dmatest_v2.py
with
29 additions
and
7 deletions
programs/dmatest_v2.py
+
29
−
7
View file @
ffcdeecf
...
...
@@ -3,14 +3,12 @@
# which is Copyright (c) 2021 Jeremy P Bentham and licensed under the Apache License, Version 2.0
import
array
import
math
import
asyncio
import
gc
import
machine
import
rp2
import
asyncio
import
uctypes
from
uctypes
import
BF_POS
,
BF_LEN
,
UINT32
,
BFUINT32
,
struct
import
gc
ADC_BASE
=
0x4004C000
...
...
@@ -61,7 +59,8 @@ def t(x):
SAMPLES
=
1000
SAMPLING_RATE_HZ
=
22000
SAMPLING_FREQ_TO_HIGH
=
-
2
SAMPLING_FREQ_TO_LOW
=
-
1
class
AdcDevice
:
def
__init__
(
self
,
device_nr
,
rate
=
SAMPLING_RATE_HZ
,
samples
=
SAMPLES
):
...
...
@@ -92,6 +91,9 @@ class AdcDevice:
self
.
_rate
=
rate
self
.
__adc_device
.
DIV_REG
=
(
48000000
//
self
.
_rate
-
1
)
<<
8
def
get_sampling_rate
(
self
):
return
self
.
_rate
def
set_sample_count
(
self
,
samples
):
async
with
self
.
__lock
:
self
.
_sample_count
=
samples
...
...
@@ -191,10 +193,13 @@ def calculate_frequency(voltages_centered, sampling_rate):
consecutive_sign
+=
1
if
len
(
counts
)
<
3
:
return
-
1
return
SAMPLING_FREQ_TO_HIGH
counts
=
counts
[
1
:
-
1
]
f
=
sampling_rate
/
(
2
*
sum
(
counts
)
/
len
(
counts
))
count_mean
=
sum
(
counts
)
/
len
(
counts
)
if
count_mean
<
5
:
return
SAMPLING_FREQ_TO_LOW
f
=
sampling_rate
/
(
2
*
count_meant
)
count
=
None
return
f
...
...
@@ -239,6 +244,23 @@ async def main():
voltage_mean
=
sum
(
voltages
)
/
SAMPLES
voltages_centered
=
[
v
-
voltage_mean
for
v
in
voltages
]
freq
=
calculate_frequency
(
voltages_centered
,
sampling_rate
=
SAMPLING_RATE_HZ
)
if
freq
==
SAMPLING_FREQ_TO_HIGH
:
rate
=
device
.
get_sampling_rate
()
if
rate
<
3
:
print
(
f
"
Cannot reduce sampling frequency rate further than
{
rate
}
"
)
continue
device
.
set_sampling_rate
(
rate
/
2.0
)
print
(
f
"
Reducing sampling rate to
{
device
.
get_sampling_rate
()
}
"
)
continue
elif
freq
==
SAMPLING_FREQ_TO_LOW
:
rate
=
device
.
get_sampling_rate
()
if
rate
>
100000
:
print
(
f
"
Cannot reduce increase frequency rate further than
{
rate
}
"
)
continue
device
.
set_sampling_rate
(
rate
*
2
)
print
(
f
"
Increasing sampling rate to
{
device
.
get_sampling_rate
()
}
"
)
continue
v_to_v
=
calculate_peak_to_peak
(
voltages_centered
)
print
(
f
"
{
freq
:
0.2
f
}
Hz,
{
v_to_v
:
0.2
f
}
V peak to peak
"
)
gc
.
collect
()
...
...
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