Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::f32::consts::PI as PI;
- use num::Complex;
- trait OutputType {}
- impl OutputType for f32 {}
- impl OutputType for Complex<f32> {}
- struct Nco<T> {
- phase: f32,
- delta_phase: f32,
- frequency: f32,
- sample_rate: f32,
- output_type: T,
- }
- impl<T: OutputType> Nco<T> {
- pub fn new(frequency: f32, sample_rate: f32, output_type: T) -> Nco<T> {
- let dp = 2.0 * PI * frequency / sample_rate;
- Nco { phase: -dp,
- delta_phase: dp,
- frequency,
- sample_rate,
- output_type, }
- }
- pub fn with_phase(mut self, phase: f32) -> Nco<T> {
- self.phase += phase;
- self
- }
- fn click(&mut self) {
- self.phase = (self.phase + self.delta_phase) % (2.0 * PI);
- }
- }
- impl Iterator for Nco<f32> {
- type Item = f32;
- fn next(&mut self) -> Option<Self::Item> {
- self.click();
- Some(self.phase.cos())
- }
- }
- impl Iterator for Nco<Complex<f32>> {
- type Item = Complex<f32>;
- fn next(&mut self) -> Option<Self::Item> {
- self.click();
- Some(Complex::from_polar(&1.0, &self.phase))
- }
- }
- fn main() {
- let real = Nco::new(440.0, 32000.0, 0f32);
- let complex = Nco::new(440.0, 32000.0, Complex::new(0f32, 0f32));
- let res = real.take(4).zip(complex.take(4))
- .all(|(x, z)| (x-z.re).abs() < 0.000001 );
- if res {
- println!("Equal");
- } else {
- println!("Not equal")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement