Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
goertzel-nostd
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
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Stephen D
goertzel-nostd
Commits
40edadc6
Commit
40edadc6
authored
9 years ago
by
Matt McPherrin
Browse files
Options
Downloads
Patches
Plain Diff
Assert we were fed the right number of samples
parent
3502149e
No related branches found
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
src/lib.rs
+8
-9
8 additions, 9 deletions
src/lib.rs
with
8 additions
and
9 deletions
src/lib.rs
+
8
−
9
View file @
40edadc6
...
...
@@ -7,7 +7,7 @@ use std::f32::consts::PI;
#[derive(Clone,
Copy)]
pub
struct
Parameters
{
// Parameters we're working with
window_size
:
u
32
,
window_size
:
u
size
,
// Precomputed value:
sine
:
f32
,
cosine
:
f32
,
...
...
@@ -16,13 +16,13 @@ pub struct Parameters {
pub
struct
Partial
{
params
:
Parameters
,
//
count: u
32
,
count
:
u
size
,
prev
:
f32
,
prevprev
:
f32
,
}
impl
Parameters
{
pub
fn
new
(
target_freq
:
f32
,
sample_rate
:
f32
,
window_size
:
u
32
)
->
Self
{
pub
fn
new
(
target_freq
:
f32
,
sample_rate
:
f32
,
window_size
:
u
size
)
->
Self
{
let
k
=
target_freq
*
(
window_size
as
f32
)
/
sample_rate
;
let
omega
=
(
f32
::
consts
::
PI
*
2.
*
k
)
/
(
window_size
as
f32
);
let
cosine
=
omega
.cos
();
...
...
@@ -35,7 +35,7 @@ impl Parameters {
}
pub
fn
start
(
self
)
->
Partial
{
Partial
{
params
:
self
,
prev
:
0.
,
prevprev
:
0.
}
Partial
{
params
:
self
,
count
:
0
,
prev
:
0.
,
prevprev
:
0.
}
}
}
...
...
@@ -46,9 +46,11 @@ impl Partial {
self
.prevprev
=
self
.prev
;
self
.prev
=
this
;
}
self
.count
+=
samples
.len
();
self
}
pub
fn
finish
(
self
)
->
(
f32
,
f32
)
{
assert_eq!
(
self
.count
,
self
.params.window_size
);
let
real
=
self
.prev
-
self
.prevprev
*
self
.params.cosine
;
let
imag
=
self
.prevprev
*
self
.params.sine
;
(
real
,
imag
)
...
...
@@ -72,9 +74,6 @@ fn sine() {
let
mut
buf
=
[
0
;
8000
];
for
&
freq
in
[
697.
,
1200.
,
1800.
,
1633.
]
.iter
()
{
// Generate a 1 second sine wave at freq hz
// Using 8khz sample rate: Generate 8k samples,
// map them into our second (zero to one):
let
step
=
1.
/
8000.
;
for
sample
in
(
0
..
8000
)
{
let
time
=
sample
as
f32
*
step
;
...
...
@@ -82,10 +81,10 @@ fn sine() {
}
let
p
=
Parameters
::
new
(
freq
,
8000.
,
8000
);
let
mag
=
p
.start
()
.add
(
&
buf
[
0
..
256
])
.finish_mag
();
let
mag
=
p
.start
()
.add
(
&
buf
[
..
])
.finish_mag
();
for
testfreq
in
(
0
..
30
)
.map
(|
x
|
(
x
*
100
)
as
f32
)
{
let
p
=
Parameters
::
new
(
testfreq
,
8000.
,
8000
);
let
testmag
=
p
.start
()
.add
(
&
buf
[
0
..
256
])
.finish_mag
();
let
testmag
=
p
.start
()
.add
(
&
buf
[
..
])
.finish_mag
();
println!
(
"{:4}: {:12.3}"
,
testfreq
,
testmag
);
if
(
freq
-
testfreq
)
.abs
()
>
100.
{
println!
(
"{} > 10*{}"
,
mag
,
testmag
);
...
...
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