Skip to content

Resolved issue of magnitude computing at the spectrum charts #821

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

demvlad
Copy link
Contributor

@demvlad demvlad commented May 2, 2025

The current spectrum has a small issue. The FFT method returns complex output for two-side spectrum. It includes Re and Im complex values parts. The current release get half of complex FFT output and uses it as magnitude data directly. The current algorithm find maximum frequency amplitude value from Re and Im values too. This math is not proper. The spectrum chart shows Re and Im values with wrong 2x frequency resolution instead of true magnitudes.
This PR resolves this issue.
The test log:
btfl_all.txt

The unfiltered roll gyro spectrum before PR:
SpectrumRollOlde
The unfiltered roll gyro spectrum by using this PR:
SpectrumRollNew
The unfiltered pitch gyro spectrum before PR:
SpectrumPitchOlde
The unfiltered pitch gyro spectrum by using this PR:
SpectrumPitchNew

The different is not big for this spectrum, but this is correct computing.

Summary by CodeRabbit

  • Bug Fixes
    • Improved accuracy of the frequency spectrum display by correctly calculating and presenting only the positive frequency magnitudes. This results in more precise and meaningful spectrum visualization for users.
  • New Features
    • Enhanced frequency spectrum visualization with finer granularity by doubling the number of bars displayed in the frequency graph.
  • Chores
    • Improved code clarity and maintainability by using defined constants for throttle value iterations and adjusting heatmap color scaling for better visual consistency.

Copy link

coderabbitai bot commented May 2, 2025

"""

Walkthrough

The internal implementations of _dataLoadFrequencyVsX, _getFlightSamplesFreqVsX, and _normalizeFft in graph_spectrum_calc.js were updated to process FFT outputs as complex pairs representing a two-sided spectrum and compute magnitude spectra limited to positive frequencies. This involved changing the FFT output array size to half the original length, pairing real and imaginary parts to calculate magnitudes, rounding FFT chunk lengths, and adjusting noise index calculations accordingly. In graph_spectrum_plot.js, the frequency graph drawing loop step was changed from 10 to 5, doubling the number of bars drawn in the frequency spectrum visualization. The heatmap scaling constant was adjusted, and a hardcoded loop limit was replaced with a defined constant. No changes were made to exported or public function signatures.

Changes

File(s) Change Summary
src/graph_spectrum_calc.js Updated _dataLoadFrequencyVsX, _getFlightSamplesFreqVsX, and _normalizeFft to compute magnitude spectra from complex FFT outputs, adjusting FFT output sizes, rounding chunk lengths, and noise index calculations. No public API changes.
src/graph_spectrum_plot.js Reduced frequency graph drawing loop step from 10 to 5 to increase frequency resolution; adjusted heatmap color intensity scaling; replaced hardcoded throttle loop limit with constant THROTTLE_VALUES_SIZE. No API changes.

Suggested labels

RN: IMPROVEMENT

Poem

In the land of spectrum, the rabbits delight,
Now only the positives shine so bright.
Complex pairs, now handled with care,
Magnitudes calculated with mathematical flair.
The FFT’s tale, now properly told—
In the warren, the signals unfold!
🐇✨
"""


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f208873 and fa3129f.

📒 Files selected for processing (2)
  • src/graph_spectrum_calc.js (6 hunks)
  • src/graph_spectrum_plot.js (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/graph_spectrum_plot.js
  • src/graph_spectrum_calc.js
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/graph_spectrum_calc.js (1)

466-466: Consider explaining the 100Hz constant

The value 100 in the noise index calculation appears to be a frequency threshold in Hz. Consider extracting this as a named constant to improve code readability.

+const NOISE_LOW_END_FREQUENCY = 100; // Hz
-const noiseLowEndIdx = 100 / maxFrequency * magnitudeLength;
+const noiseLowEndIdx = NOISE_LOW_END_FREQUENCY / maxFrequency * magnitudeLength;
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3639e69 and cb2256b.

📒 Files selected for processing (1)
  • src/graph_spectrum_calc.js (1 hunks)
🧰 Additional context used
🪛 ESLint
src/graph_spectrum_calc.js

[error] 467-468: Missing semicolon.

(semi)

🔇 Additional comments (4)
src/graph_spectrum_calc.js (4)

461-464: Improved FFT handling for spectrum calculation!

This change correctly acknowledges that FFT output consists of complex value pairs (real, imaginary) representing a two-sided spectrum. Computing magnitudes for just one side of the spectrum is the right approach as the other side is redundant for real input signals.


471-479: Correctly calculating magnitude from complex values

This implementation properly computes the magnitude from the complex FFT output using Math.hypot(re, im), which is equivalent to sqrt(re² + im²). This gives the true amplitude of each frequency component rather than using real and imaginary parts separately.


481-481: Properly scaling maxNoiseIdx to frequency

The calculation now correctly scales the maximum noise index using the magnitude length instead of the full FFT length, which aligns with the reduced spectrum size.


486-488: Returning the correct magnitude array and length

The return object now includes the magnitudes array and updated length, ensuring downstream code receives properly normalized FFT data.

@haslinghuis haslinghuis added this to the 4.0.0 milestone May 2, 2025
@demvlad
Copy link
Contributor Author

demvlad commented May 2, 2025

The last commit restores spectrum chart points count, because magnitudes have half points count than it were before.

@nerdCopter
Copy link
Member

please post the BBL used for these screenshots.

@demvlad
Copy link
Contributor Author

demvlad commented May 2, 2025

please post the BBL used for these screenshots.

btfl_all.txt

@demvlad
Copy link
Contributor Author

demvlad commented May 2, 2025

The GraphSpectrumCalc._dataLoadFrequencyVsX method what is used in throttle and rpm spectrum has this issue too. Will improve soon.

@demvlad demvlad marked this pull request as draft May 2, 2025 16:53
@demvlad
Copy link
Contributor Author

demvlad commented May 2, 2025

The unfiltered pitch gyro by RPM spectrum before PR:
SpectrumRPM_olde
The unfiltered pitch gyro spectrum by using this PR:
SpectrumRPM_new

It exists a small difference between separated re, im values before, and sqrt(re^2 + im^2) values after PR.

@demvlad demvlad marked this pull request as ready for review May 2, 2025 19:36
@nerdCopter
Copy link
Member

just dropping this here, potentially unhelpful, or potentially miscalculated.

Spoiler

DEMVLAD_BBE_PR821_btfl_all 01_step_response_stacked_plot_0 5s
DEMVLAD_BBE_PR821_btfl_all 02_GyroVsUnfilt_stacked
DEMVLAD_BBE_PR821_btfl_all 02_PIDsum_PIDerror_Setpoint_stacked
DEMVLAD_BBE_PR821_btfl_all 02_SetpointVsGyro_stacked
DEMVLAD_BBE_PR821_btfl_all 02_SetpointVsPIDsum_stacked
DEMVLAD_BBE_PR821_btfl_all 02_step_response_stacked_plot_0 5s
DEMVLAD_BBE_PR821_btfl_all 03_GyroVsUnfilt_stacked
DEMVLAD_BBE_PR821_btfl_all 03_PIDsum_PIDerror_Setpoint_stacked
DEMVLAD_BBE_PR821_btfl_all 03_SetpointVsGyro_stacked
DEMVLAD_BBE_PR821_btfl_all 03_SetpointVsPIDsum_stacked
DEMVLAD_BBE_PR821_btfl_all 03_step_response_stacked_plot_0 5s

DEMVLAD_BBE_PR821_btfl_all 01_GyroVsUnfilt_stacked
DEMVLAD_BBE_PR821_btfl_all 01_PIDsum_PIDerror_Setpoint_stacked
DEMVLAD_BBE_PR821_btfl_all 01_SetpointVsGyro_stacked
DEMVLAD_BBE_PR821_btfl_all 01_SetpointVsPIDsum_stacked

@mituritsyn
Copy link
Contributor

mituritsyn commented May 3, 2025

Sorry, I can't add any valuable comments as not familiar with this math. But if you could find the source of this horisontal artefacts - it would be great)
P.S they are not appear in PTB plots
image

@demvlad
Copy link
Contributor Author

demvlad commented May 3, 2025

But if you could find the source of this horisontal artefacts - it would be great) P.S they are not appear in PTB plots

Yes, it is interesting. I'll figure it out.

@demvlad
Copy link
Contributor Author

demvlad commented May 6, 2025

But if you could find the source of this horisontal artefacts - it would be great) P.S they are not appear in PTB plots

Seems, PTB has not spectrum by RPM.

I have bin checked. The issues at the spectrums top are not just math error. But its have some reasons.

  1. There are rare, much high rpm freq points, what spectrum "average" has 1 spectrum row only
  2. We can see identical spectrum rows for these high rpm values at the chart. Because current algorithm uses this one spectrum row for each 4 rpm values at moment. We can see "wide row" when we have 4 motors rpm difference as 1 rpm axise step value.
  3. The spectrum row values have wide values range and rude linear scaling at plot. The linear scaling can not show the all values range at the chart.

What can try to do:

  • use fft with segmentation, to get smoother rows
  • maybe drop high rows what have average by 1 row.
  • use PSD in dBm for these charts too, to show the all spectrum magnitudes values range at the chart

@demvlad demvlad marked this pull request as draft May 6, 2025 13:20
@mituritsyn
Copy link
Contributor

Seems, PTB has not spectrum by RPM.

needs to be clarified:

  • these artifacts were initially noticed on freq vs throttle plots
  • I noticed that the artifact occurs in the area where we have little data - so it could be averaging or smoothing issue

Copy link

sonarqubecloud bot commented May 6, 2025

@demvlad demvlad marked this pull request as ready for review May 6, 2025 13:59
@demvlad
Copy link
Contributor Author

demvlad commented May 6, 2025

The true magnitude is more then separated Re and Im values. Therefore the "heat map" charts scaling was decreased, to save origin spectrum charts brightness.
The spectrums by RPM.
This PR:
rpm_new
The master branch:
rpm_olde

@demvlad
Copy link
Contributor Author

demvlad commented May 6, 2025

  • these artifacts were initially noticed on freq vs throttle plots
  • I noticed that the artifact occurs in the area where we have little data - so it could be averaging or smoothing issue

Will try to use fft with samples segmentation for current chart and will try PSD values too.

@demvlad
Copy link
Contributor Author

demvlad commented May 6, 2025

This PR resolves some math issues only. But the charts view change looks as cosmetic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants