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
fc9419e5
Commit
fc9419e5
authored
9 years ago
by
Matt McPherrin
Browse files
Options
Downloads
Patches
Plain Diff
A basic implementation, mostly untested!
parent
115f9773
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
+65
-1
65 additions, 1 deletion
src/lib.rs
with
65 additions
and
1 deletion
src/lib.rs
+
65
−
1
View file @
fc9419e5
// This (implicitly/naively) uses a rectangular window. Some way to set up a window function
// will be needed probably -- if mutating your samples before calling this isn't sufficient.
/// Set up parameters (and some precomputed values for those).
#[derive(Clone,
Copy)]
pub
struct
Parameters
{
// Parameters we're working with
window_size
:
u32
,
// Precomputed value:
sine
:
f32
,
cosine
:
f32
,
term_coefficient
:
f32
,
}
pub
struct
Partial
{
params
:
Parameters
,
//count: u32,
prev
:
f32
,
prevprev
:
f32
,
}
impl
Parameters
{
pub
fn
new
(
target_freq
:
f32
,
sample_rate
:
f32
,
window_size
:
u32
)
->
Self
{
use
std
::
f32
;
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
();
Parameters
{
window_size
:
window_size
,
sine
:
omega
.sin
(),
cosine
:
cosine
,
term_coefficient
:
2.
*
cosine
,
}
}
pub
fn
start
(
self
)
->
Partial
{
Partial
{
params
:
self
,
prev
:
0.
,
prevprev
:
0.
}
}
}
impl
Partial
{
pub
fn
add
(
mut
self
,
samples
:
&
[
u16
])
->
Self
{
for
&
sample
in
samples
{
let
this
=
self
.params.term_coefficient
*
self
.prev
-
self
.prevprev
+
(
sample
as
f32
);
self
.prevprev
=
self
.prev
;
self
.prev
=
this
;
}
self
}
pub
fn
finish
(
self
)
->
(
f32
,
f32
)
{
let
real
=
self
.prev
-
self
.prevprev
*
self
.params.cosine
;
let
imag
=
self
.prevprev
*
self
.params.sine
;
(
real
,
imag
)
}
pub
fn
finish_mag
(
self
)
->
f32
{
let
(
real
,
imag
)
=
self
.finish
();
(
real
*
real
+
imag
*
imag
)
.sqrt
()
}
}
#[test]
fn
it_works
()
{
fn
zero_data
()
{
let
p
=
Parameters
::
new
(
1800.
,
44100.
,
256
);
assert!
(
p
.start
()
.add
(
&
[
0
;
256
])
.finish_mag
()
==
0.
);
assert!
(
p
.start
()
.add
(
&
[
0
;
128
])
.add
(
&
[
0
;
128
])
.finish_mag
()
==
0.
);
}
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