sharpie7 / circuitjs1

Electronic Circuit Simulator in the Browser
GNU General Public License v2.0
2.23k stars 621 forks source link

Feature request - quadrature oscillator #890

Open marekm72 opened 10 months ago

marekm72 commented 10 months ago

It would be a "black box" with 3 inputs (amplitude, frequency, phase) and two outputs (I/Q, sin/cos, Re/Im whatever name you like). I thought about building it from parts already available in the simulator, but it doesn't seem to be trivial - if phase is simulated with an integrator (so phase is continuous even with step changes in frequency) and then sin/cos calculated from it, long running simulations will have increasing floating point numeric errors (even though it would work perfectly in theoretical math), so the position angle on the circle needs to wrap around within a limited range of 2*pi. It could be extended to output configurable number of phases, like 3-phase 120 deg apart. But any number of phases can be made from 2 phases externally by simple linear combination. One possible use I was thinking about - simulate a PMSG (permanent mangnet synchronous generator) with a wind turbine, as a black box with 2 input terminals and 3 output phases (voltage and frequency both proportional to angular speed). Input voltage = angular speed [rad/s], input current = torque [Nm], input power same as output power, rotational inertia could be simulated by parallel capacitance on the input, input torque from the turbine would be a current source. Torque vs angular speed and wind speed is a separate complex issue depending on the turbine design (efficiency vs tip speed ratio) outside of this.

marekm72 commented 10 months ago

This proposed simple implementation seems to work as a subcircuit with two voltage inputs relative to ground for 2*pi*frequency (F) and peak voltage (V), and two isolated output voltage sources for sine/In-phase/Real and cosine/Quadrature/Imaginary. Built from two ideal 1F capacitors (the "cosine" one initially charged to 1V), two ideal controlled current and two voltage sources, all 4 sources controlled by multiply of two voltages relative to ground. Please feel free to improve and use it any way you want, I release it as public domain.

circuit-20230827-1522

$ 1 0.000005 10.20027730826997 50 5 50 5e-11 213 592 176 640 176 0 2 ab 213 592 272 640 272 0 2 ab 207 928 176 960 176 4 I\p 207 928 208 960 208 4 I- 207 928 272 960 272 4 Q\p 207 928 304 960 304 4 Q- 212 832 176 864 176 0 2 ab 212 832 272 880 272 0 2 ab g 688 208 704 208 0 0 g 688 272 704 272 0 0 w 592 272 592 240 0 w 592 240 736 240 0 w 688 176 736 176 0 w 736 176 736 240 0 w 592 176 592 144 0 w 592 144 752 144 0 w 752 144 752 272 0 w 752 272 752 304 0 w 752 304 688 304 0 c 784 176 784 208 0 1 0 0 c 784 272 784 304 0 1 1 1 w 736 176 784 176 0 w 784 176 832 176 0 w 752 272 784 272 0 w 784 272 832 272 0 g 784 208 784 224 0 0 g 784 304 784 320 0 0 w 832 208 816 208 0 w 816 208 816 304 0 w 816 304 832 304 0 w 592 208 576 208 0 w 576 208 576 304 0 w 576 304 592 304 0 207 576 208 544 208 4 F w 816 304 816 352 0 w 816 352 576 352 0 207 576 352 544 352 4 V