Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,150,841 members, 7,810,242 topics. Date: Saturday, 27 April 2024 at 01:49 AM |
Nairaland Forum / Science/Technology / Programming / Inverter Pic Program (932 Views)
Forget About PHCN! Carriable 200ah 200,000mah Power Bank Battery/inverter / Enjoy Uninterrupted Power Supply With Inverter And Solar Backup System / Gen Or Inverter ? Which Is Better For Programming (2) (3) (4)
Inverter Pic Program by okete(m): 7:26pm On Nov 03, 2014 |
I need someone very good in programming to help me in a program that can give pure sine wave |
Re: Inverter Pic Program by naijatechworld: 7:35pm On Nov 03, 2014 |
okete: You didnt say in what specific programming language, so Ima give you options: In Java: package example.fixedfreqsine; import java.nio.ByteBuffer; import javax.sound.sampled.*; public class FixedFreqSine { //This is just an example - you would want to handle LineUnavailable properly... public static void main(String[] args) throws InterruptedException, LineUnavailableException { final int SAMPLING_RATE = 44100; // Audio sampling rate final int SAMPLE_SIZE = 2; // Audio sample size in bytes SourceDataLine line; double fFreq = 440; // Frequency of sine wave in hz //Position through the sine wave as a percentage (i.e. 0 to 1 is 0 to 2*PI) double fCyclePosition = 0; //Open up audio output, using 44100hz sampling rate, 16 bit samples, mono, and big // endian byte ordering AudioFormat format = new AudioFormat(SAMPLING_RATE, 16, 1, true, true); DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); if (!AudioSystem.isLineSupported(info)){ System.out.println("Line matching " + info + " is not supported." throw new LineUnavailableException(); } line = (SourceDataLine)AudioSystem.getLine(info); line.open(format); line.start(); // Make our buffer size match audio system's buffer ByteBuffer cBuf = ByteBuffer.allocate(line.getBufferSize()); int ctSamplesTotal = SAMPLING_RATE*5; // Output for roughly 5 seconds //On each pass main loop fills the available free space in the audio buffer //Main loop creates audio samples for sine wave, runs until we tell the thread to exit //Each sample is spaced 1/SAMPLING_RATE apart in time while (ctSamplesTotal>0) { double fCycleInc = fFreq/SAMPLING_RATE; // Fraction of cycle between samples cBuf.clear(); // Discard samples from previous pass // Figure out how many samples we can add int ctSamplesThisPass = line.available()/SAMPLE_SIZE; for (int i=0; i < ctSamplesThisPass; i++) { cBuf.putShort((short)(Short.MAX_VALUE * Math.sin(2*Math.PI * fCyclePosition))); fCyclePosition += fCycleInc; if (fCyclePosition > 1) fCyclePosition -= 1; } //Write sine samples to the line buffer. If the audio buffer is full, this will // block until there is room (we never write more samples than buffer will hold) line.write(cBuf.array(), 0, cBuf.position()); ctSamplesTotal -= ctSamplesThisPass; // Update total number of samples written //Wait until the buffer is at least half empty before we add more while (line.getBufferSize()/2 < line.available()) Thread.sleep(1); } //Done playing the whole waveform, now wait until the queued samples finish //playing, then clean up and exit line.drain(); line.close(); } } |
Re: Inverter Pic Program by terrymason(m): 9:09pm On Nov 03, 2014 |
Do they program and get sine wave? Cux I know that they have some special ic responsible for sine wave production....(Search google oga). |
Re: Inverter Pic Program by africanaija: 11:10pm On Nov 03, 2014 |
okete: Since you didn't specify what programming language you want the answer in, here are a few: In PHP - http://php.net/manual/en/function.sin.php <?php // Precision depends on your precision directive echo sin(deg2rad(60)); // 0.866025403 ... echo sin(60); // -0.304810621 ... ?> In Python - http://interactivepython.org/courselib/static/thinkcspy/Labs/sinlab.html import math y = math.sin(math.radians(90)) print(y) In Java - http://www.wolinlabs.com/blog/java.sine.wave.html package example.fixedfreqsine; import java.nio.ByteBuffer; import javax.sound.sampled.*; public class FixedFreqSine { //This is just an example - you would want to handle LineUnavailable properly... public static void main(String[] args) throws InterruptedException, LineUnavailableException { final int SAMPLING_RATE = 44100; // Audio sampling rate final int SAMPLE_SIZE = 2; // Audio sample size in bytes SourceDataLine line; double fFreq = 440; // Frequency of sine wave in hz //Position through the sine wave as a percentage (i.e. 0 to 1 is 0 to 2*PI) double fCyclePosition = 0; //Open up audio output, using 44100hz sampling rate, 16 bit samples, mono, and big // endian byte ordering AudioFormat format = new AudioFormat(SAMPLING_RATE, 16, 1, true, true); DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); if (!AudioSystem.isLineSupported(info)){ System.out.println("Line matching " + info + " is not supported." throw new LineUnavailableException(); } line = (SourceDataLine)AudioSystem.getLine(info); line.open(format); line.start(); // Make our buffer size match audio system's buffer ByteBuffer cBuf = ByteBuffer.allocate(line.getBufferSize()); int ctSamplesTotal = SAMPLING_RATE*5; // Output for roughly 5 seconds //On each pass main loop fills the available free space in the audio buffer //Main loop creates audio samples for sine wave, runs until we tell the thread to exit //Each sample is spaced 1/SAMPLING_RATE apart in time while (ctSamplesTotal>0) { double fCycleInc = fFreq/SAMPLING_RATE; // Fraction of cycle between samples cBuf.clear(); // Discard samples from previous pass // Figure out how many samples we can add int ctSamplesThisPass = line.available()/SAMPLE_SIZE; for (int i=0; i < ctSamplesThisPass; i++) { cBuf.putShort((short)(Short.MAX_VALUE * Math.sin(2*Math.PI * fCyclePosition))); fCyclePosition += fCycleInc; if (fCyclePosition > 1) fCyclePosition -= 1; } //Write sine samples to the line buffer. If the audio buffer is full, this will // block until there is room (we never write more samples than buffer will hold) line.write(cBuf.array(), 0, cBuf.position()); ctSamplesTotal -= ctSamplesThisPass; // Update total number of samples written //Wait until the buffer is at least half empty before we add more while (line.getBufferSize()/2 < line.available()) Thread.sleep(1); } //Done playing the whole waveform, now wait until the queued samples finish //playing, then clean up and exit line.drain(); line.close(); } } |
(1) (Reply)
Visual Basic Timer Please Help / Nigerian Government To Compel Foreign Firms To Use Locally Developed Software / Interesting Programming Quotes
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 20 |