Producing Waveforms using Wave Generator module in the PSLab Android App

This blog will demonstrate how to produce different waveforms using the Wave Generator module in the PSLab android app and view them on the Oscilloscope. The Wave Generator in PSLab android app is simple to use and has a UI which is similar to physical commodity wave generators. It is capable of producing different waveforms like sine, sawtooth and square wave. Apparatus Required Before getting started with the wave generator we require the following items: PSLab device An android phone with PSLab app installed in it. USB cable (Mini B) OTG(On the Go) wire Some connecting wires having pins at both ends Understanding the Wave Generator Pins Let me briefly explain the use of the pins that are going to be used in the Wave generator module: S1 and S2 pins The PSLab device contains two pins (S1, S2) which are capable of producing two independent analog waveforms (sine,  sawtooth) having different frequencies and phase offset. The frequency range is from 10Hz to 5Khz. SQR1, SQR2, SQR3 and SQR4 pin The SQR1 pin is used for producing the square waveform and all the SQ pins can be used together to produce four different PWM signal having the same frequency. These PWM signal can have a different duty cycle and phase. CH1, CH2 and CH3 pin The CH pins are used by the oscilloscope in the  PSLab android app to monitor waveform signals produced by the wave generator pins. They can be used together to simultaneously monitor multiple waveforms. Setting up the Device We need to connect the PSLab device with the mobile phone as shown in Figure 2 which can be done by following steps: Connect a micro USB(Mini B) to the PSLab device. Connect the other end of the micro USB cable to the OTG. Connect the OTG to the phone. Producing Waveforms Now, once the device has been properly connected to the device (which is shown at the top right corner of the app), then in the instruments page scroll down to the Wave Generator card and click on it to open the WaveGenerator activity. Here you will see a screen like shown in Figure 4 containing two monitors and a controlling panel with lots of buttons. Here the Waveform panel is used to control the S1 and S2 pins whose properties are shown on the left monitor screen and the Digital panel is used to control the SQR pins whose properties are shown on the right monitor screen. For sine/sawtooth wave: Connect the S1 pin to the CH1 pin using a connecting wire, then in the Waveform panel select the Wave1 button, choose the type of waveform(either sine or sawtooth), then click on the Freq button to change the frequency of the wave, then use the Seek bar or the up/down arrow buttons to change the value of frequency and then press the set button to set the frequency for the S1 pin as shown below: Now, click the view button at bottom right corner, this will directly…

Continue ReadingProducing Waveforms using Wave Generator module in the PSLab Android App

Creating the Mockup for Wave Generator using Moqups

PSLab is a versatile device that can be used as a waveform signal generator, as it is capable of producing different types of waves. It provides a cheap and portable alternative as compared to the physical wave generators available in markets. The current UI of the Wave Generator present in the PSLab app as shown in Figure 1 is very basic and makes use of some basic UI components which are not able to properly expose all the functionalities of the wave generator. This affects the user experience negatively. In this blog, we will discuss creating a mockup for the PSLab wave generator using modern design to improve the UI/UX of the app. I will try to keep the layout simple enough so that users can easily understand it.  Understanding the Functionality of Waveform Generator We can observe from the image that PSLab provide: Two pins named W1 and W2 which are capable of producing two different sine or saw-tooth waves having different characteristics simultaneously. Four Pins named SQ1, SQ2, SQ3, SQ4 which are capable of producing Square Wave and Pulse Width modulation signals. It also allows us to change the properties of these waves such as frequency, phase, duty cycle. Before starting with the mockup a physical commodity Waveform Signal Generator as shown in Figure 3 was inspected. Most waveform generators in the market have a control panel with buttons and a monitor screen so that the user can view the changes while controlling the characteristics of the wave. I will try to create a layout similar to this in following steps by dividing it into two halves: Upper half containing monitor screens Lower half containing controlling panel having buttons Steps for creating the mockup Step 1: Choosing the Tool The tool that we will be using here to create a mockup is moqups[1]. I have chosen this software because:- It is an online tool and doesn’t require it to be set up first It is very efficient and powerful tool for creating mobile or web wireframes. It doesn't require any prior knowledge of programming language. It has a palette to drag & drop UI elements like Buttons, Textboxes, Checkboxes etc. on the left-hand side, allows modifying the features of each element (here on the right) and other options at the top related to prototyping, previewing etc as shown in figure 4. Step 2: Creating the base of the layout The mockup tool moqups[1] have a built-in template of Android screen which we can drag and drop from the palette and use it as our base. Step 3: Creating the Lower Half Now as discussed above I will create controlling panel containing buttons in the lower half of the screen, for this I will drag and drop buttons from the palette on the right-hand side and design them according to the theme of the app that is making their border round shape, changing their color to match the theme color and editing text in them. As the PSLab has…

Continue ReadingCreating the Mockup for Wave Generator using Moqups

Filling Audio Buffer to Generate Waves in the PSLab Android App

The PSLab Android App works as an oscilloscope and a wave generator using the audio jack of the Android device. The implementation of the oscilloscope in the Android device using the in-built mic has been discussed in the blog post “Using the Audio Jack to make an Oscilloscope in the PSLab Android App” and the same has been discussed in the context of wave generator in the blog post “Implement Wave Generation Functionality in the PSLab Android App”. This post is a continuation of the post related to the implementation of wave generation functionality in the PSLab Android App. In this post, the subject matter of discussion is the way to fill the audio buffer so that the resulting wave generated is either a Sine Wave, a Square Wave or a Sawtooth Wave. The resultant audio buffer would be played using the AudioTrack API of Android to generate the corresponding wave. The waves we are trying to generate are periodic waves. Periodic Wave: A wave whose displacement has a periodic variation with respect to time or distance, or both. Thus, the problem reduces to generating a pulse which will constitute a single time period of the wave. Suppose we want to generate a sine wave; if we generate a continuous stream of pulses as illustrated in the image below, we would get a continuous sine wave. This is the main concept that we shall try to implement using code. Initialise AudioTrack Object AudioTrack object is initialised using the following parameters: STREAM TYPE: Type of stream like STREAM_SYSTEM, STREAM_MUSIC, STREAM_RING, etc. For wave generation purposes we are using stream music. Every stream has its own maximum and minimum volume level.   SAMPLING RATE: It is the rate at which the source samples the audio signal. BUFFER SIZE IN BYTES: Total size of the internal buffer in bytes from where the audio data is read for playback. MODES: There are two modes- MODE_STATIC: Audio data is transferred from Java to the native layer only once before the audio starts playing. MODE_STREAM: Audio data is streamed from Java to the native layer as audio is being played. getMinBufferSize() returns the estimated minimum buffer size required for an AudioTrack object to be created in the MODE_STREAM mode. minTrackBufferSize = AudioTrack.getMinBufferSize(SAMPLING_RATE, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT); audioTrack = new AudioTrack(       AudioManager.STREAM_MUSIC,       SAMPLING_RATE,       AudioFormat.CHANNEL_OUT_MONO,       AudioFormat.ENCODING_PCM_16BIT,       minTrackBufferSize,       AudioTrack.MODE_STREAM); Fill Audio Buffer to Generate Sine Wave Depending on the values in the audio buffer, the wave is generated by the AudioTrack object. Therefore, to generate a specific kind of wave, we need to fill the audio buffer with some specific values. The values are governed by the wave equation of the signal that we want to generate. public short[] createBuffer(int frequency) {   short[] buffer = new short[minTrackBufferSize];   double f = frequency;   double q = 0;   double level = 16384;   final double K = 2.0 * Math.PI / SAMPLING_RATE;   for (int i = 0; i < minTrackBufferSize; i++) {         f += (frequency - f) / 4096.0;         q += (q < Math.PI) ?…

Continue ReadingFilling Audio Buffer to Generate Waves in the PSLab Android App

Implement Wave Generation Functionality in The PSLab Android App

The PSLab Android App works as an Oscilloscope using the audio jack of Android device. The implementation for the scope using in-built mic is discussed in the post Using the Audio Jack to make an Oscilloscope in the PSLab Android App. Another application which can be implemented by hacking the audio jack is Wave Generation. We can generate different types of signals on the wires connected to the audio jack using the Android APIs that control the Audio Hardware. In this post, I will discuss about how we can generate wave by using the Android APIs for controlling the audio hardware. Configuration of Audio Jack for Wave Generation Simply cut open the wire of a cheap pair of earphones to gain control of its terminals and attach alligator pins by soldering or any other hack(jugaad) that you can think of. After you are done with the tinkering of the earphone jack, it should look something like shown in the image below. If your earphones had mic, it would have an extra wire for mic input. In any general pair of earphones the wire configuration is almost the same as shown in the image below. Android APIs for Controlling Audio Hardware AudioRecord and AudioTrack are the two classes in Android that manages recording and playback respectively. For Wave Generation application we only need AudioTrack class. Creating an AudioTrack object: We need the following parameters to initialise an AudioTrack object. STREAM TYPE: Type of stream like STREAM_SYSTEM, STREAM_MUSIC, STREAM_RING, etc. For wave generation purpose we are using stream music. Every stream has its own maximum and minimum volume level. SAMPLING RATE: it is the rate at which source samples the audio signal. BUFFER SIZE IN BYTES: total size in bytes of the internal buffer from where the audio data is read for playback. MODES: There are two modes MODE_STATIC: Audio data is transferred from Java to native layer only once before the audio starts playing. MODE_STREAM: Audio data is streamed from Java to native layer as audio is being played. getMinBufferSize() returns the estimated minimum buffer size required for an AudioTrack object to be created in the MODE_STREAM mode. private int minTrackBufferSize; private static final int SAMPLING_RATE = 44100; minTrackBufferSize = AudioTrack.getMinBufferSize(SAMPLING_RATE, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT); audioTrack = new AudioTrack(       AudioManager.STREAM_MUSIC,       SAMPLING_RATE,       AudioFormat.CHANNEL_OUT_MONO,       AudioFormat.ENCODING_PCM_16BIT,       minTrackBufferSize,       AudioTrack.MODE_STREAM); Function createBuffer() creates the audio buffer that is played using the audio track object i.e audio track object would write this buffer on playback stream. Function below fills random values in the buffer due to which a random signal is generated. If we want to generate some specific wave like Square Wave, Sine Wave, Triangular Wave, we have to fill the buffer accordingly. public short[] createBuffer(int frequency) {   // generating a random buffer for now   short[] buffer = new short[minTrackBufferSize];   for (int i = 0; i < minTrackBufferSize; i++) {       buffer[i] = (short) (random.nextInt(32767) + (-32768));   }   return buffer; } We created a write() method and passed the audio buffer created in above step as an argument to…

Continue ReadingImplement Wave Generation Functionality in The PSLab Android App