Skip to content

Instantly share code, notes, and snippets.

@SteveTrewick
Created June 28, 2023 14:27
Show Gist options
  • Save SteveTrewick/3ac61c7c92d23396ee90e63004953baf to your computer and use it in GitHub Desktop.
Save SteveTrewick/3ac61c7c92d23396ee90e63004953baf to your computer and use it in GitHub Desktop.
Generate the I and Q components for a local oscillator
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