Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8798231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:02:36+00:00 2026-06-14T00:02:36+00:00

I am experiencing precision loss when im using doubles and i cant seem to

  • 0

I am experiencing precision loss when im using doubles and i cant seem to find where the precision is lost.
I am writing a software synthesizer and for some reason the input frequency for an oscillator(sound-wave generator) gets heavily aliased somewhere along the way.
The oscillator is supposed to generate a triangle waveform and the correct frequency is passed to the method but when the result is played in the speakers i can clearly hear that the sound snaps to different frequencies.

I am using NAudio (http://naudio.codeplex.com/) and this method is run once for every sample i want to generate.

Here is the code:

double counter = 0;
int samplecounter = 0;

public double GetNextTriangle(double frequency)
{
    double samplesperwave = (double)Parent.WaveFormat.SampleRate / frequency;
    double length = (double)samplecounter / samplesperwave;
    if (length.CompareTo(0.25) == -1)
    {
        counter = length * 4.0;
    }
    else if (length.CompareTo(0.75) == -1)
    {
        counter = 1.0 - (length - 0.25) * 4.0;
    }
    else
    {
        counter = -1.0 + (length - 0.75) * 4.0;
    }
    samplecounter++;
    samplecounter = samplecounter > samplesperwave ? 0 : samplecounter;
    return counter;
}

Thanks in advance! //Mats

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:02:38+00:00Added an answer on June 14, 2026 at 12:02 am

    The floating point operations here are well conditionned. But you may see an alisaing problem related to sampling if 1/frequency is not a multiple of 1/sampleRate.

    Try this matlab code if you can

    sampleRate=5000;
    frequency=700;
    sampleperwave=sampleRate/frequency;
    samplecounter=0:floor(sampleperwave);
    samplecounter=repmat(samplecounter,1,5);
    length=samplecounter/sampleperwave;
    wave=-1+4*(length-0.75);
    wave(length<0.75)=1-4*(length(length<0.75)-0.25);
    wave(length<0.25)=4*length(length<0.25);
    figure; stem(wave); hold on; plot(wave,'r')
    

    discontinuity

    You could try to declare samplecounter as double and increment it with a modulo

    samplecounter++;
    samplecounter = samplecounter % samplesperwave;
    

    Or back in matlab

    samplecounter=0:length(samplecounter)-1;
    samplecounter=rem(samplecounter,sampleperwave);
    len=samplecounter/sampleperwave;
    wave=-1+4*(len-0.75);
    wave(len<0.75)=1-4*(len(len<0.75)-0.25);
    wave(len<0.25)=4*len(len<0.25);
    figure; stem(wave); hold on; plot(wave,'r')
    

    aliasing

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm experiencing some strange behaviour when using a stylus with swing. I am interpreting
I am experiencing some trouble using the Python 2.6.5 xml.etree.ElementTree library. In particular, if
I'm experiencing some problems when using the default date:difference EXSLT template, provided at http://www.exslt.org/date/functions/difference/index.html
I am experiencing a problem with some Javascript - please see my site and
Some reasons i am experiencing this issue on my website. When the website loads
I am experiencing a fatal error while using artisan on the (fantastic) Laravel PHP
I'm currently experiencing some problems with my server. I have a pool which is
After experiencing some performances issues on my client side, we decided to give a
I am experiencing some confusion in the use and purpose of Spring's DataBinder and
I've been experiencing segfaults when running some C++ code. I've isolated the problem to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.