Phase, non-sinusoidal references and harmonics
In single-phase synchronous detection we saw that a finite mean can return the amplitude we are looking for, or a completely different value, depending on the frequencies of the signal and of the reference. Now I want to separate two problems: what happens if the reference does not follow the signal exactly, and what changes if instead of the sine wave I use a square wave, as might happen in a switching detector?
1. The relative phase is not always constant
I take up the S07 model again, with input x(t)=A sin(2πfst+φ) in volts and a dimensionless unit reference r(t)=sin(2πfrt). In this section fs is the signal frequency; for sampling I will use the notation fsamp. I define Δf=fs−fr. The normalized product u(t)=2x(t)r(t) is:
\[ u(t) = A\cos(2\pi\Delta f\,t+\varphi) – A\cos\left[2\pi(f_s+f_r)t+\varphi\right]. \]
When Δf=0 and the sum term is suppressed, the reading is A cos φ, a signed projection. When Δf≠0, the relative phase evolves as φ+2πΔft: even with zero initial phase the reading is not necessarily positive. A low-pass filter that attenuates the sum term but lets the difference term through returns a component oscillating at |Δf|, not a DC value.
2. Frequency detuning and finite averaging
For a continuous rectangular mean from the origin to duration T I use the same sinc convention as in the previous article, sinc(v)=sin(πv)/(πv), with sinc(0)=1. Applying the S07 identity to the two cosines I get:
\[ \frac{A_X(T)}{A} = \operatorname{sinc}(\Delta f\,T)\cos(\pi\Delta f\,T+\varphi) – \operatorname{sinc}\left[(f_s+f_r)T\right]\cos\left[\pi(f_s+f_r)T+\varphi\right]. \]
The formula is exact in continuous time. For the numerical check I instead use the mean over the samples, with the discrete response already derived in S07; I do not confuse the sinc with the finite sum of the samples. If the sum term is negligible and φ=0, the first part becomes sin(2πΔfT)/(2πΔfT). A frequency offset can therefore make the reading decrease or even invert without the physical amplitude of the signal having changed.
I choose a sinusoidal input of 1 V peak at 205 Hz and a unit reference at 200 Hz, both with zero initial phase. I add no noise or other interfering tones. The product contains a component at 5 Hz and one at 405 Hz; the relative phase completes one turn in 200 ms. I sample at 2 kHz and consider 400 samples from 0 to 199.5 ms. I will compare four nominal windows, of 50, 100, 150 and 200 ms, with the same starting record. These are conditions chosen for the mathematical test, not experimental bench data.
Try it yourself with GX-TXT
I carried out the experiments that follow with GX-TXT, free software you can install on Linux. There is no need to take my numbers on trust: follow the steps, rebuild the signals and the references, and compare your readings with mine. Then change something — the frequency offset, the initial phase, the duty cycle of the square wave — and see how the response to the harmonics changes. It is the best way to really understand what a lock-in correlates, and to have some fun too.
3. I check frequency detuning in the Scientific Worksheet
I create an independent session, LockIn_08_fase_armoniche, in the GX-TXT DEV112 P012 copy aligned with the micropatches applicable up to MP051. I open Viewers (1), search for Worksheet in Search (2) and choose Worksheet (3). I save the initial sheet.

In Functions (4) I search for and select numeric.uniform_grid (5). I set start=0 (6), stop=0.1995 (7), spacing=400 (8), Options=mode="count" (9), Output cell=A1 (10). I press Preview fields (11), wait for the fields to be captured, choose x in Result field (12) and press Apply result (13).

Column A contains the time grid in seconds, with a step of 0.0005 s. The nominal duration of the whole record is 200 ms; the last sample is at 199.5 ms.
I search for and select signal.generate.sine (14). I set time=A1:A400 (15), frequency=200 (16), modulation=empty() (17), Options=amplitude=1 phase_rad=0 (18), Output cell=B1 (19). I press Preview fields (20), wait, choose value_real (21) and apply the result (22). B is the dimensionless reference.

To generate the input, I leave the other parameters unchanged, change frequency to 205 (23) and Output cell=C1 (24). I repeat Preview fields (25), check that Result field is value_real (26) and press Apply result (27). C represents the input in volts with a peak amplitude of 1 V.

In the Cell & Formula panel I select D1 (28), enter (29) and apply (30) the formula of the already normalized product:
=numeric.multiply(numeric.multiply(B1:B400,C1:C400),2)

I have not yet applied a low-pass filter: D contains both the 5 Hz component and the 405 Hz one. To observe the effect of the window, in E1 (31) I enter (32) and apply (33) four means of the same product:
=vector(numeric.mean(D1:D100),numeric.mean(D1:D200),numeric.mean(D1:D300),numeric.mean(D1:D400))

In F1 (34) I enter (35) and apply (36) the nominal durations in milliseconds, corresponding to 100, 200, 300 and 400 samples respectively:
=vector(50,100,150,200)

3.1. I plot the four readings
Procedure in GX-TXT: chart, publication and labels (Figures 8–10)
I select E1:F4 (37), open Chart (38) and press Add selection (39).

I set Title=Frequency detuning - 5 Hz (40), X column=F (41), Y columns=E (42), First row=1 (43), Last row=4 (44), Kind=scatter (45), Panels=overlay (46), Marker=circle (47). I choose limits X=0–220 ms (48–49) and Y=−0.3–0.8 V (50–51). I press Apply (52) and, after the regeneration, Publish immutable Graph to Plots (53).

In Plots I select p0001 (54), open Presentation (55), Title and labels (56), write Observation window (ms) in X / bottom label (57) and Normalized reading (V) in Y Left label (58). I save (59), choose Render (60) and Rerender selected (61). I wait for the completion confirmation.


The results are four readings, not a continuous response curve: that is why I do not connect the markers. The original CSV contains the abscissa in ms and the ordinates in V, with the precision of the Dataset.
| Samples | Duration (ms) | Normalized reading (V) |
|---|---|---|
| 100 | 50 | 0.629837223 |
| 200 | 100 | 3.2485 × 10−15 |
| 300 | 150 | −0.209945741 |
| 400 | 200 | −2.8341 × 10−15 |
With 50 ms the 5 Hz term completes a quarter of a period and the reading is positive; with 150 ms it completes three quarters of a period and the reading is negative. The sign indicates the mean projection with respect to the reference, not a negative physical amplitude. The 405 Hz component also takes part in the finite sum: I did not use the approximation that removes it.
At 200 ms both terms complete an integer number of periods (1 and 81). At 100 ms, instead, they complete 0.5 and 40.5 periods respectively. In the particular case of zero initial phase, the discrete sum of the two cosines over 200 samples is the same for both (1), so their difference is zero. Residues of the order of 10−15 V are compatible with numerical rounding errors. I do not claim that a half-period window cancels any initial phase: for a different phase, in general, this does not happen.
A longer mean does not fix a detuned reference
I have therefore verified that a longer duration does not guarantee recovering the amplitude when the reference is detuned. A sufficiently long mean also rejects the signal I wanted to measure, because its product no longer generates a DC component. This is a deterministic test with an ideal sinusoidal reference; the square-wave reference remains to be studied.
4. The square-wave reference: the problem of odd harmonics
A switching detector can use a bipolar reference that is +1 in the first half of the period and −1 in the second. For the same fundamental frequency f₀, with zero phase and a 50% duty cycle, the Fourier series is:
\[ r(t) = \frac{4}{\pi}\left[\sin(\omega_0 t) + \frac{\sin(3\omega_0 t)}{3} + \frac{\sin(5\omega_0 t)}{5} + \dots\right], \qquad \omega_0 = 2\pi f_0. \]
For a sinusoidal input of amplitude A in phase with one of the odd harmonics k, the mean over whole periods of the product is 2A/(πk). So I multiply the mean by π/2 to obtain a reading normalized to the fundamental. The unit sinusoidal reference instead requires the factor 2. Under ideal conditions, the former responds to odd harmonics even if the input frequency does not coincide with the fundamental frequency of the reference; the latter does not have this harmonic sensitivity.
4.1. Preparing the signals
I choose f₀=200 Hz and fsamp=20 kHz, i.e. 100 samples per period. The window contains four periods (400 samples, nominal duration 20 ms). I choose the centres of the intervals, starting from 25 µs and up to 19.975 ms, to avoid falling exactly on the edges of the square wave. In Functions I select numeric.uniform_grid (62), set start=0.000025 (63), stop=0.019975 (64), spacing=400 (65), Options=mode="count" (66), Output cell=G1 (67). I run Preview fields (68), select x (69) and Apply result (70).

I select signal.generate.square (71), time=G1:G400 (72), frequency=200 (73), modulation=empty() (74), Options=amplitude=1 phase_rad=0 duty=0.5 (75), Output cell=H1 (76); Preview fields (77), value_real (78), Apply result (79). This is the first generation, which will later be discarded because of a software defect.

I select signal.generate.sine (80), use time=G1:G400 (81), frequency=200 (82), modulation=empty() (83), Options=amplitude=1 phase_rad=0 (84) and Output cell=I1 (85). I run Preview fields (86), choose value_real (87) and Apply result (88). Column I is also the unit sinusoidal reference.

I repeat the sinusoidal generation with the same parameters, changing the frequency and the output cell: 400 Hz in J1 (89–90), 600 Hz in K1 (91–92), 1000 Hz in L1 (93–94). All inputs have a peak amplitude of 1 V and zero phase.

In Cell & Formula I select M1 (95), enter (96) and apply (97) the vector of frequencies; in N1 (98) I enter (99) and apply (100) the four means normalized for the sinusoidal reference:
=vector(200,400,600,1000)
=vector(2*numeric.mean(numeric.multiply(I1:I400,I1:I400)),2*numeric.mean(numeric.multiply(I1:I400,J1:J400)),2*numeric.mean(numeric.multiply(I1:I400,K1:K400)),2*numeric.mean(numeric.multiply(I1:I400,L1:L400)))

4.2. Checking the phase origin and the necessary corrections
In O1 I calculate the preliminary readings with reference H, using the factor π/2:
=vector(1.5707963267948966*numeric.mean(numeric.multiply(H1:H400,I1:I400)),1.5707963267948966*numeric.mean(numeric.multiply(H1:H400,J1:J400)),1.5707963267948966*numeric.mean(numeric.multiply(H1:H400,K1:K400)),1.5707963267948966*numeric.mean(numeric.multiply(H1:H400,L1:L400)))
The preliminary result shows a residue at 400 Hz of about 0.000493 V, instead of zero. I look directly at the edge: at time 2.525 ms the square reference H51 is still +1, while I51 is already negative (101). The cause is not a physical property of the lock-in, but the different time origin used by the generators. Before the fix, square/pulse used t−t₁, while sine used t. The shift also brought some samples onto the intended discontinuities.

Reproducibility note (DEV112 P012). During saving, bug 293 also emerged, related to the limit of the Worksheet transport graph. MP052 extends the lossless recovery already provided by MP046 to this specific error; the 19 states of the history were verified after saving and reopening. MP053 corrects the phase origin of the square/pulse generators and documents the convention. With GX-TXT closed, install the previous applicable micropatches and then MP052 and MP053 in order, extracting the ZIPs according to the README files into the framework directory and keeping the relative paths. Restart. With MP053 the phase is referred to t=0 for square/pulse too: results from previous versions with a non-zero start time must be regenerated if they depend on that convention. The case t₁=0 and the chirp generator are not modified.
I do not use the preliminary data H and O for the conclusions; I keep them in the session. I repeat from the GUI the generation of the square reference with the parameters of the test (102), this time with Output cell=P1 (103). After Preview fields and selecting value_real, I apply (104).

I select Q1 (105), enter (106) and apply (107) the four normalized means obtained using P:
=vector(1.5707963267948966*numeric.mean(numeric.multiply(P1:P400,I1:I400)),1.5707963267948966*numeric.mean(numeric.multiply(P1:P400,J1:J400)),1.5707963267948966*numeric.mean(numeric.multiply(P1:P400,K1:K400)),1.5707963267948966*numeric.mean(numeric.multiply(P1:P400,L1:L400)))

4.3. Graphical and numerical comparison
Procedure in GX-TXT: chart, publication and labels (Figures 20–21)
I select M1:Q4 and, in Chart, add the selection. I configure chart_2 (108), Title=Harmonic sensitivity (109), X column=M (110), Y columns=N Q (111), First row=1 (112), Last row=4 (113), Kind=scatter (114), Panels=overlay (115), Marker=circle (116). Limits X from 0 to 1100 Hz (117–118) and Y from −0.1 to 1.1 V (119–120). I press Apply (121) and Publish immutable Graph to Plots (122). In this way I exclude the preliminary readings O.

In Plots I select p0002, open Presentation (123), Title and labels (124), enter Input frequency (Hz) (125) and Normalized reading (V) (126), save (127). From the Render menu (128) I choose Rerender selected (129).


The original CSV contains four pairs of normalized readings, in volts. I do not join the points: these are tests at single frequencies, not a continuous response curve.
| Frequency (Hz) | Sinusoidal ref. (V) | Square ref. (V) | Ideal continuous square (V) |
|---|---|---|---|
| 200 | 1.000000 | 1.000164512 | 1 |
| 400 | ≈ 0 | ≈ 0 | 0 |
| 600 | ≈ 0 | 0.333827325 | 1/3 |
| 1000 | ≈ 0 | 0.200824841 | 1/5 |
The difference from 1/k is the one expected from the discrete mean over 100 samples per period. Indeed, setting P=100, for odd k the product with the square wave doubles the sum of the sines over the first P/2 centred samples. The sum of those sines is 1/sin(kπ/P) for the odd orders considered. The normalized reading is therefore:
\[ \frac{A_{\text{reading}}}{A} = \frac{\pi/P}{\sin(k\pi/P)} \qquad (k = 1, 3, 5;\ P = 100). \]
As P grows, it tends to 1/k. For k=2, in the same test the mean vanishes by symmetry. The numerical residues at frequencies that give a theoretically zero response are of the order of 10−16 V. This explains why I must not impose exactly 1/3 and 1/5 on results obtained with a finite grid. I kept amplitude, initial phase and window the same for all inputs; the values are not bench measurements nor an estimate of the response at any phase.
A square-wave reference also hears the odd harmonics
The square-wave reference simplifies the realization of a switching multiplier, but introduces sensitivity to input components that an ideal sinusoidal reference would reject. The next check concerns the loss of symmetry as the duty cycle varies.
5. What changes if the duty cycle is not 50%?
The disappearance of the even harmonics depends on the symmetry of the square wave. If r(t+T₀/2)=−r(t), the contributions of the two halves of the period cancel for the even orders and the mean is also zero. I change only the fraction of the period D in which the reference is high: from D=0.5 I go to D=0.25. I keep the ±1 amplitude, the 200 Hz fundamental frequency, the phase origin and the same samples.
With θ=2πf₀t, the reference rD(θ) is +1 from θ=0 to 2πD (upper end excluded) and −1 in the rest of the period. I define the coefficients of the series rD(θ)=c₀+∑[ak cos(kθ)+bk sin(kθ)]. Integrating over the two intervals I get:
\[ c_0 = 2D-1, \qquad a_k = \frac{2\sin(2\pi kD)}{\pi k}, \qquad b_k = \frac{2\left[1-\cos(2\pi kD)\right]}{\pi k}, \quad k\ge 1. \]
For an input x=A sin(kθ+φ), averaged over whole periods of the reference, the reading with the factor π/2 adopted in section 4 is:
\[ X_k = \frac{\pi}{2}\langle x\,r_D\rangle = \frac{A\left[\left(1-\cos(2\pi kD)\right)\cos\varphi + \sin(2\pi kD)\sin\varphi\right]}{2k}. \]
With φ=0, D=0.25, the expected responses in the continuous limit are A/2 at the fundamental, A/2 at the second harmonic, A/6 at the third and A/10 at the fifth. I keep the calibration of the symmetric case to also highlight the change of the projection at the fundamental. I do not confuse this in-phase projection with the magnitude of the fundamental: for D=0.25, a₁ and b₁ are both 2/π, so the maximum normalized response as the phase varies would be A/√2, not A/2. I fixed the rising edge at θ=0: changing D also changes the centre of the high interval.
5.1. I generate the asymmetric square wave
I reopen the same Workspace after MP053. In Functions (130) I select signal.generate.square (131), time=G1:G400 (132), frequency=200 (133), modulation=empty() (134), Options=amplitude=1 phase_rad=0 duty=0.25 (135). I set Output cell=R1 (136), press Preview fields (137), choose value_real (138) and Apply result (139). I do not overwrite P, the corrected 50% reference.

In Cell & Formula I use Go to to select S1 (140), enter (141) and apply (142) the following formula. The four inputs I, J, K and L have already been generated in the previous section.
=vector(1.5707963267948966*numeric.mean(numeric.multiply(R1:R400,I1:I400)),1.5707963267948966*numeric.mean(numeric.multiply(R1:R400,J1:J400)),1.5707963267948966*numeric.mean(numeric.multiply(R1:R400,K1:K400)),1.5707963267948966*numeric.mean(numeric.multiply(R1:R400,L1:L400)))

I also calculate the mean of the two references, choosing T1 (143), entering (144) and applying (145):
=vector(numeric.mean(P1:P400),numeric.mean(R1:R400))

An asymmetric reference lets the offset through
I read 0 and −0.5 respectively. The symmetric reference does not convert a constant input offset into a DC value at the ideal output. For the asymmetric one, instead, an offset Voff would contribute to the normalized reading by (π/2)(2D−1)Voff, i.e. −πVoff/4 in the case D=0.25. This is a consequence of the verified mean value; I did not separately simulate an input with an offset.
5.2. I compare the four projections
Procedure in GX-TXT: comparison chart, publication and labels (Figures 26–28)
I select M1:S4 (146), open Chart (147) and press Add selection (148). I set Title=Harmonic sensitivity vs duty cycle (149), X column=M (150), Y columns=Q S (151), First row=1 (152), Last row=4 (153). Kind=scatter (154), Panels=overlay (155), Marker=circle (156); X minimum=0 and maximum=1100 Hz (157–158), Y minimum=−0.1 and maximum=1.1 V (159–160). I press Apply (161), wait for the regeneration and choose Publish immutable Graph to Plots (162). Columns R and P contain the waveforms; I do not include them among the ordinates. Q and S contain only the readings obtained with the corrected reference.


In Plots I select p0003 (163), open Presentation (164) and Title and labels (165). I set Input frequency (Hz) (166) and Normalized reading (V) (167), save (168), then choose Render (169) and Rerender selected (170).


The most evident difference is at 400 Hz: the blue marker stays on zero, while the red one is at about 0.5 V. At the odd frequencies the red is at half the height of the blue. The shared scale makes the values directly comparable; the small differences due to the grid cannot be resolved visually in this graph. I do not connect the markers, because these are four discrete tests.
From the original CSV of the published Dataset I obtain the following four pairs. In both numerical columns I use the same factor π/2.
| Frequency (Hz) | 50% (V) | 25% (V) | Ideal continuous 25% (V) |
|---|---|---|---|
| 200 | 1.000164512 | 0.500082256 | 1/2 |
| 400 | ≈ 0 | 0.500329138 | 1/2 |
| 600 | 0.333827325 | 0.166913663 | 1/6 |
| 1000 | 0.200824841 | 0.100412420 | 1/10 |
The difference from the continuous values can be explained by the discrete grid. For Pc=100 centred samples per period and m=D Pc an integer, the sum of the sines over the first m samples is sin²(kπm/Pc)/sin(kπ/Pc). Since for the orders considered the sum over the whole period is zero, the normalized reading at zero phase becomes:
\[ \frac{X_k}{A} = \frac{\pi}{P_c}\,\frac{\sin^2(k\pi D)}{\sin(k\pi/P_c)}. \]
The formula is valid for the orders considered and for the coherent, centred sampling chosen; in the limit of an ever finer grid it coincides with the continuous projection. The readings at the fundamental and at the odd harmonics are halved compared with the 50% case; above all, a strong response appears at 400 Hz. It is not a numerical error or a new distortion of the generator: it is a consequence of the loss of symmetry. It is not correct, however, to conclude that any non-symmetric duty cycle makes all even harmonics non-zero: for example at D=1/4 the orders that are multiples of four have a zero coefficient.
6. Conclusions: the shape of the reference is part of the measurement
A lock-in does not simply measure everything that oscillates at the nominal frequency of the reference. It measures a correlation, and the nature of that correlation depends both on the frequency and phase and on the waveform used.
With a sinusoidal reference, a signal at 205 Hz compared with a reference at 200 Hz produces a difference at 5 Hz. The mean can therefore decrease, change sign or vanish without any change in the physical amplitude of the input. A finite output filter also affects the reading, as seen in S07.
With a 50% bipolar square reference and π/2 normalization, the correlation is not limited to the fundamental: the third and fifth harmonics produce a response. I verified the difference between the continuous prediction and that of the discrete grid, keeping the frequency and initial phase of the inputs identical.
Bringing the duty cycle to 25%, with the rising edge fixed, the projection at the fundamental is halved compared with 50%, a strong response at the second harmonic appears and the mean of the reference becomes −0.5. An input offset can therefore contribute to the result. These consequences must not be confused with the software defect in the phase origin, which I corrected before accepting the results.
The results at a glance
- 5 Hz detuning (1 V at 205 Hz, reference at 200 Hz): reading 0.6298 V at 50 ms, practically zero at 100 and 200 ms, −0.2099 V at 150 ms.
- 50% square reference, π/2 normalization: 1.000165 at the fundamental, ≈ 0 at the second harmonic, 0.333827 at the third and 0.200825 at the fifth (ideal continuous values 1, 0, 1/3, 1/5).
- 25% duty cycle: 0.500082, 0.500329, 0.166914 and 0.100412 at 200, 400, 600 and 1000 Hz; mean of the reference −0.5.
- MP052 (bug 293) and MP053 (bug 292, phase origin of the square/pulse generators) are needed to reproduce the results on P012.
From the circuit point of view I will have to check the symmetry of the switch drive, the DC component, the phase alignment and possible interference at the harmonics of the reference frequency. The checks presented here concern ideal waveforms and coherent sampling; they do not yet include jitter, finite switching times, component offsets or noise. They are not a measurement of bench performance.
There also remains the problem already seen in single-phase detection: how to obtain the amplitude of a signal when the phase is not known? In the next article, Quadrature detection: X, Y, amplitude and phase, I will study quadrature detection, using two references shifted by 90° to obtain the X and Y components, the amplitude and the phase.