# Reducing harmonics in the resonance testing signal

**URL:** <https://klipper.discourse.group/t/reducing-harmonics-in-the-resonance-testing-signal/8518>\
**Category:** Developers\
**Created:** [May 21, 2023, 12:24am UTC](https://klipper.discourse.group/t/reducing-harmonics-in-the-resonance-testing-signal/8518 "2023-05-21T00:24:21Z")\
**Posts on this page:** 1\
**Showing post:** 19

<div class="post-metadata">

**Author:** ![Piezo](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/piezo/32/1110_2.png) [@Piezo](https://klipper.discourse.group/u/Piezo)\
**Post date:** [May 27, 2023, 11:34pm UTC](https://klipper.discourse.group/t/reducing-harmonics-in-the-resonance-testing-signal/8518/19 "2023-05-27T23:34:26Z")

</div>

> [@dmbutyugin](#):
>
> I have a slight suspicion that there may be another reason for that. Basically, with some constant cruising velocity the test should generate the acceleration signal something like: (+a, 0, +a, -a, 0, -a, …). I suspect this could cause even harmonics to actually amplify.

Analytically, I didn’t find any even harmonic. Both waveforms have odd symmetries at +/- T/4, where the acceleration sign flips, or at the middle of the cruise segment. This cancel out the inner product with sine bases at `2*n*frequency`.

> [@dmbutyugin](#):
>
> If you want to, you could post your patch to the mainline Klipper code here as a diff (I suspect it’s very small), then I could generate an ‘ideal’ acceleration signal for you for further debugging.

You can find the patch in the first post of this thread. This should be enough to replicate the wave-form. A simulated acceleration signal would be very welcome, thanks. 🙂

* * *

To experiment with the effect of the stepper position, I modified the `resonance_tester` code to sweep positions instead of frequencies. At each iteration a very small offset (83.33 nm) is added to the backstroke. Over the course of the 6 min long test, the toolhead travels _3 electrical cycles_, 12 steps, or 2.4 mm.

 ![image](https://global.discourse-cdn.com/free1/uploads/klipper/original/2X/5/50431c08d21a0310f84ec372556c13f390296f87.jpeg)  
(I tweaked the visualization with `vmin, vmax = np.quantile(pdata, [0.25, 0.999])` and `noverlap=M - M//4`.)

- The second and fourth harmonics go in and out twice per step.
- The 3rd harmonic, which is present in the ideal signal, also shows some modulation, but once per step.
- Overall the positions with the least amount of harmonics seems to be half-steps.
- The electrical cycle is marked by these wide band noise patterns at 150-400 Hz. Otherwise, there is no other pattern that is different on each of the 4 steps, [as suggested here](https://klipper.discourse.group/t/proposal-forbidden-velocities-for-printer-steppers/3995/17). But these results might be specific to this test (and 40Hz frequency).
- The 6-10th harmonics seems to show faster paced patterns, but it’s quite faint and low resolution. I could try to stretch the test even more in time.

The spectrogram above used the modified wave form. This one is using the original square-wave:

 ![image](https://global.discourse-cdn.com/free1/uploads/klipper/original/2X/c/ccbac537180ab9f0407bfe0c00545b8bcdfda778.jpeg)  
Very similar beside the stronger 3rd harmonic.

**Edit:** I repeated the test above but over 4 steps for a duration of 10 min. I plotted the power of the first 5 harmonics over the central position of the oscillation:

 ![amp](https://global.discourse-cdn.com/free1/uploads/klipper/original/2X/f/f0a687fb4f614e1b82f5d8b095d2a9258801a4ec.png)  
It appears that the pattern on the 2nd harmonic has a period of 2 steps.

I’m not sure where I’m going with this. I guess this shows that harmonics are generated by the stepper themself, so it’s less relevant to try to remove them from the test signal. On the other hand, there is less effect on even harmonics, likely because they are already present in the test signal, showing room for improvement.

---

_[View the full topic](https://klipper.discourse.group/t/reducing-harmonics-in-the-resonance-testing-signal/8518)._
