Created
June 28, 2023 14:27
-
-
Save SteveTrewick/3ac61c7c92d23396ee90e63004953baf to your computer and use it in GitHub Desktop.
Generate the I and Q components for a local oscillator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
/* | |
Generate the I and Q components for a local oscillator | |
*/ | |
public class IQ_Oscillator { | |
let tau = 2 * Double.pi | |
let dphi : Double | |
var phi : Double = 0 | |
public init(frequency: Double, sampleRate: Double) { | |
// compute phase increment per sample | |
dphi = (tau / sampleRate) * frequency | |
} | |
public func sample() -> (i: Double, q: Double) { | |
let i = cos(phi) | |
let q = sin(phi) | |
phi += dphi | |
if phi >= tau { phi -= tau } // don't let this accumulate, error becmes large | |
return (i, q) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment