How do I fix slight distortion in background in generating sine wave

I'm getting a slight distortion in the background when I run the following code, the distortion is very slight and sounds like buzzing. Because of its subtle nature it makes believe there is some sort of aliasing going on with the byte casting:
Note: code assumes (for now) that the data is in big endian.
Output:
PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, big-endian
(sound)
     public static void playFreq(AudioFormat audioFormat, double frequency, SourceDataLine sourceDataLine)
          System.out.println(audioFormat);
          double sampleRate = audioFormat.getSampleRate();
          int sampleSizeInBytes = audioFormat.getSampleSizeInBits() / 8;
          int channels = audioFormat.getChannels();
          byte audioBuffer[] = new byte[(int)Math.pow(2.0, 19.0) * channels * sampleSizeInBytes];
          for ( int i = 0; i < audioBuffer.length; i+=sampleSizeInBytes*channels )
               int wave = (int) (127.0 * Math.sin( 2.0 * Math.PI * frequency * i / (sampleRate * sampleSizeInBytes * channels) )  );
               //wave = (wave > 0 ? 127 : -127);
               if ( channels == 1 )
                    if ( sampleSizeInBytes == 1 )
                         audioBuffer[i] = (byte) (wave);
                    else if ( sampleSizeInBytes == 2 )
                         audioBuffer[i] = (byte) (wave);
                         audioBuffer[i+1] = (byte)(wave >>> 8);
               else if ( channels == 2 )
                    if ( sampleSizeInBytes == 1 )
                         audioBuffer[i] = (byte) (wave);
                         audioBuffer[i+1] = (byte) (wave);
                    else if ( sampleSizeInBytes == 2 )
                         audioBuffer[i] = (byte) (wave);
                         audioBuffer[i+1] = (byte)(wave >>> 8);
                         audioBuffer[i+2] = (byte) (wave);
                         audioBuffer[i+3] = (byte)(wave >>> 8);
          sourceDataLine.write(audioBuffer, 0, audioBuffer.length);
     }

The data was actually LITTLE-endian, it become apparent when you use a larger amplitude (say 2^15).

Similar Messages

  • How to run the basic list in background to generate secondary.

    How to run the basic list in background to generate secondary and spool for the secondary.Please note that Basic list will run in foreground.

    Problem solved.

  • How to dynamically adjust Frequency, Phase and Amplitude for a Generated Sine Wave in C/C++?

    Hi,
    I wonder if anybody know how to dynamically adjust Frequency, Phase and Amplitude for a Generated Sine Wave in C/C++? This is a part of my project: I need to dynamically generate a sine wave voltage signal by some calculation using the data from a simultaneous analog input and another signal.
    Your help will be appreciated so much!
    Marlon

    Hello Marlon,
    Here is an example of changing frequency and ampliude for an E-series device in Labwindows/CVI. The programming should be similar in C for your S-Series device. As is says on the linked webpage above, this process is going to be very processor intensive since you will continually be creating a buffer.
    Nicholas C
    National Instruments
    Applications Engineering

  • How do I Fix DDS Generated Sine Wave That Produces a Periodic Square Wave?

    Hello,
    I'm an undergraduate researcher with almost no experience dealing with FPGA semantics and other National Instruments hardware in general.  Part of my project revolves around creating a sine wave signal via the FPGA on an sbRIO-9632 that is output via the board's analog I/O pins, and I found it convenient to adapt the code provided by the National Instruments tutorials (http://zone.ni.com/devzone/cda/epd/p/id/6066) to have it run on an sbRIO FPGA.  The problem I'm encountering right now is that the actual output I'm seeing is a square wave as opposed to the smooth sine wave that was expected, and I've exhausted all my ideas on possible ways to debug this issue.  Can anyone please give me feedback if they've seen this problem before or offer other suggestions on fixing this?
    I'm currently running LabVIEW 2009 with the appropriate real-time and FPGA modules on a 64-bit Windows 7 machine.  Again, the hardware is an sbRIO-9632 complete with analog I/O.  I appreciate it everyone!

    I am also having this issue and have not been able to find a solution in the forums. I am somewhat of a rookie so pardon me if I seem ignorant in any way. I am working on a graduate project where I need to produce some sine waves and I am getting more of a square wave when I output it and measure on the scope. The program I am running is quite simple at the moment to attempt to discover any errors by process of elimination. I have the sine wave generator in a while loop outputting to an AO port. I am using the cRIO 9022 with the 9269 AO module. I will attach screen shots of my code and my oscope image
    Attachments:
    sine wave.jpg ‏76 KB
    code for project.PNG ‏169 KB

  • How to change the phase by 90 deg of a sine wave

    Hi
    Does any one know how to change the phase of a sine wave by 90 degrees.
    Mal

    Hi Malkoba,
    Thank you for your post.
    Having looked at your VI I can say that you have created the correct inputs to the 'Sine Waveform.vi'.
    However the dial you had created had no affect as it was not wired into it the 'Sine Waveform.vi' on the block diagram.To fix the problem I have wired the 'Phase' dial into the 'phase' terminal of the waveform generator. This now changes the phase of the sine wave during runtime.
    The dial also now has a digital display (numeric box below it)- this can be used to read the dial's value, or feed in specific values into the control.
    Please find a modified version of your VI below.
    Regards,
    Field Sales Engineer | National Instruments | UK & Ireland
    Attachments:
    NI_phase shift sine.vi ‏31 KB

  • How can i measure only the valley voltages of a sine wave in labview 7 express evaluation version?

    I am trying to convince my company to send me to purchase labview and send me to classes for labview.
    I am trying to put together a small demo for them using the Labview 7 express evaluatin copy that I recieved.
    What I am wondering is how can filter out everything besides the valley peaks on a sine wave.
    What I am trying to accomplish, is create an example showing that Labview can sameple from a sine wave, read about 5 "valley" points of the wave, evaluate these based on a value that i can define, and signal pass or fail.
    attached is a picture of what i have so far
    Ryan
    LV 7.1
    Attachments:
    progress.bmp ‏3841 KB

    Hi,
    I built a VI using LabVIEW 7.0 that detects the valleys in a sine wave. It uses the 'Peak detector' VI that I believe is available in the evaluation version.
    (Functions palette >> All Functions >> Analyze >> Signal Processing >> Time Domain >> Peak Detector)
    I have attached a VI as well as the screen shots its front panel and block diagram.
    I hope this helps.
    Sincerely,
    Feroz
    Attachments:
    valley_detector_bd.jpg ‏36 KB
    valley_detector_fp.jpg ‏72 KB
    valley_detector.vi ‏29 KB

  • How do I fix a distorted speaker?

    Well my battery died and when I turned it back on the sound sounds distorted. This has happened to me before on a number of occasions and I contacted support and they told me to turn it on and off again and it worked but it persists and now turning it off doesn't work, what shall I do?

    Might be. If you want to skip the rest of the troubleshooting steps and go to the Genius Bar, I wouldn't blame you. I did get a speaker replaced on an iPhone once for $68 but don't know if they'll do that for an iPad. Best of luck to you.

  • How to measure time difference between zero crossing of a sine wave and rise time of a pulse on a same graph?

    I have a 50Hz sine wave and a pulse signal on a same graph. The phase difference between two is between 0-90 degrees.
    Now I need to calculate the time difference between (when the sine wave crosses zero volts) and (when the pulse rises). The frequency will stay approximately same for both signals.
    The application is for a three phase generator. In simple words, when the time difference between the zero-crossing of sine wave and the pulse rises increases, that means that the load on the generator has increases.
    I am a beginner user of LabView (version 9, 2009), maybe it is a very simple problem but I've been pulling my hair out for the last few days and coudln't figure anything out. Any help would be greatly appreciated. I am using DAQ USB-6008 to measure these voltages and pulse from the generator and a sensor
    I have attached a jpg (a graph that i just made with excel to explain). The time 't' is what I am trying to measure
    Cheers
    Awais 
    Message Edited by awais.h on 03-30-2010 11:20 PM
    Message Edited by awais.h on 03-30-2010 11:21 PM
    Solved!
    Go to Solution.

    Hi
    Thanks for the code but I'm afraid it won't work. Like you said the probability of choosing a value that is on both graphs may not happen. This is something that I would like the code to do automatically.
    But I did use the previous code posted and made some modifications to it and it seems to work perfectly. Now the next thing is to be able to get just that one value that tells you the first time difference.
    Here is what I get using that code.
    As you can see from the t Values. I only need the one that is highlighted. If there is a way to filter out the rest it would be great.
    I was thinking of a while loop and as soon as the value is higher than 3E-5 it would store the number and stop the loop, but I'm not too familiar with arrays in labview.
    Here is the the code modified.
    As you can see, it wasn't that big of a modification and it still is your code.
    I will keep trying.
    Thanks for the help
    Attachments:
    FinalShockSpeed.vi ‏55 KB

  • Problem with distortion with DAQ Generating Sine wave

    Hello,
    I have a little problem with my VI who is made for driving a biphased stepper motor:
    When I change dynamically the output frequency of my DAQ Generaor, I have some weird effect (see picture). It happend progressivelly like this :
    (the start frequency doesn't matter)
    95Hz - nothing
    95-100Hz progressivelly grow
    100Hz :
    100-105 Hz degressive
    105Hz - nothing
    So I generate 2 phases with a rate of 1M and 100k for sampling. All other stuff are set with the same settings.
    My device is an NI USB-6356 (1.25MS/s/chanel)
    I have tried to set all things at 100k but when I do that my motor won't turn.
    Does anyone know why it does this ?
    Thank you in advance
    Attachments:
    Sub_Mesure1.vi ‏238 KB

    Hello Mike,
    I use 2 simulate signal setting in sine : see Capture2.png
    and both DAQ Gen and Acq : Capture3.png / Capture4.png
    Attachments:
    Capture2.PNG ‏39 KB
    Capture3.PNG ‏70 KB
    Capture4.PNG ‏73 KB

  • How to restart after being connected to a non-pure sine wave inverter?

    I took my MacBook Pro to the motor home last night and set it up for a trip today.  I forgot the computer was on and I plugged it in.. While traveling today it was on the inverter power which is not a pure sine wave inverter.  Tonight the computer opens but I can't activate anything with the mouse and when I try the little color wheel starts spinning.  I have also tried starting in safe mode by holding down the shift key while starting and I get the same result.  I don't know what else to do and I am hoping I haven't fried something!
    Stu

    Have you tried the basics, SMC and PRAM resets?
    http://support.apple.com/kb/HT3964
    http://support.apple.com/kb/ht1379
    Ciao.

  • How to count the number of cycles in sine wave

    Hi,
    I am new to Labview.  I have labview 8.2 version and my dataacquisition is NIDAQPAD6015.  I could write a program to generate a sine wave of desired frequency through simulate signal. My question is how do i measure the number of cycles for this sine wave as soon as I run the program. Hope my question  is clear.
    Ravi Mokirala

    To find the offset (which in this case is the Y value midpoint around which the sine wave is symetrical):
    Capture more than one cycle.  Find the min and max values.  Subtract Min from Max and divide by 2.  Then subtract this result from the Max value (or add the result to the Min value).  This is the offset.
    To count the number of cycles where the offset is unknown, just use the first data point value as a reference.  If the next data point is more positive, then count the number of times that the first data point is repeated and the next data point is more positive.  Of course with DBL data type, you probably would not get the exact same value as the first data point, so you need to use In Range with a very tiny limit.  It all depends on your signal amplitude, frequency, and sampling rate.
    Example:  Lets say your first data point value is 0.5 and the 2nd point is 0.55.  You need to look for another value that is between 0.48 and 0.52.  Then read the very next data point value.  If it is greater than the previous value, add one to the cycle count.  If it is less than, you are at half a cycle, do not add to the count.
    - tbob
    Inventor of the WORM Global

  • How to generate finite cycles of sine wave using daq mx in vc++

    Hi,
    How to generate finite cycles of sine wave or for finite duration in vc++ using hardware timer without any sleepfunction in the code.
    Thanks & Regards,
    satya

    Hello Elizabeth,
    Thanks for the response.
    1) I am using NI 6040E PXI.
    2) I am using NI-DAQMx 8.3
    3) Measurement Studio is 6.0.0.105
    4) Operating system is Windows XP.
    I want to generate sine wave for finite duration previously in traditional daq there was function called wave_op.In which we can mention number of cycles of sine wave we wanted to generate. That function is not available in DAQMx so we have to use sleep function to generate finite period. Any hardware control function to control generation of finite cycles please let me know.
    Thanks & Regards,
    satya

  • For some reason, I cannot change my desktop background no matter how many times I delete the "active" folder.  It's as if it's locked.  Any ideas how I can fix it?

    For some reason, I cannot change my desktop background no matter how many times I delete the "active" folder.  It's as if it's locked.  Any ideas how I can fix it?

    Yes it sounds like you have a corrupted preference file.
    The setting you make in individual programs get saved to a Preference file in your Users/Library/Preferences folder.
    All you have to do is drag out the old file to the desktop, reboot and load the program and the preference file gets rebuilt without your preferences which you reset.
    For your case I beleive the preference file is
    com.apple.desktop.plist

  • My mac book pro login screen background wont show and only shows a light white/blue as the background how do i fix it ?

    my mac book pro mountion lion system  login screen background wont show and only shows a light white/blue as the background how can i fix it ? i tried everything from loginox to onyx and it still wont solve the problem. instead of the default dark grey startup screen  the background is white with a blue tint to it. the only time that the default background shows is when i log off and relog in

    Welcome to the Apple Support Communities
    I think that your Mac detects your hard disk, but the problem is that your new hard disk doesn't contain any operating system, so your Mac doesn't find any bootable partition and you see a question mark.
    If your Mac came with DVDs, insert the Mac OS X Install DVD and hold the C key while your Mac is starting. Then, just install Mac OS X. If the hard disk isn't prepared to install Mac OS X, you will have to erase it using "Mac OS Extended (Journaled)". See > http://pondini.org/OSX/DU1.html

  • My iTunes background has gone gray and black. What did I do and how do I fix the issue?

    My iTunes background has gone gray and black. What did I do and how do I fix the issue?

    Had the same problem.  Go to Users/Library/Preferences.  Discard every file that begins with com.apple.iTunes...

Maybe you are looking for

  • Need selection screen values in Search help user exit in same report

    Hi Experts, I am using Logical Data Base PNP. Created a custom search help to validate the records for IT000 based on the selection screen dates. Enter the DATEs for Data Selection Period and Person selection period in the selection screen. When I fo

  • How to set the data providers in WAD 7.0 dynamically.

    Hi All, I have 2 queries, 1 wad template and 2 data providers for each query.WAD template has one analysis item.When i run the query-1, DP1 should be set to analysis item dynamically and DP2 when i execute query 2. Can any one help on this issue with

  • EREC : Recruiter Dash Board- My applications.

    Hi, We are on E-Rec EHP4. While an external Candidate applies to a requisition via Reference code and as a recruiter when we check on My Applications in Dashboard. It shows most of the information except "CV" with an error message "Not all informatio

  • MiniApps & IAC iview

    Hi all, Can anyone tell me the difference between SAP miniapps iview and IAC iview in Enterprise Portal 6.0. Regards Vasu

  • Audio is/goes out of sync

    Hi guys, Brand new user to Premiere Pro CS 5.5 I took some clips off my Galaxy Nexus that I shot the other day and wanted to cut them into a short video so I imported them into Premiere. When I play them in Premiere the audio appears to be in sync at