Showing posts with label signal processing. Show all posts
Showing posts with label signal processing. Show all posts

Sunday, October 15, 2017

ABC Logo Lissajous Curve

Over the last couple of days I've seen a few people mention how similar the ABC Australian logo is to the new Disney Movies Anywhere service. I don't know much about the legal side of things, but I thought an explanation of why the ABC logo looks the way it does might be interesting.

The shape of logo is called a Lissajous figure or curve. The shape is generated by a parametric equation where the x and y coordinates are sinusoidal.  The frequency and phase relationship between the two equations for x and y determine the shape.  In the case of the ABC logo, the frequency of the y coordinate is 3 times that of the x coordinate and there is a 90 degree phase shift (I'll clarify this later) applied the y equation.

$$$x=cos(t)$$$ and $$$y=cos(3t+\pi/2)$$$

To make things clearer I've put together an animation. As the vertical bar sweeps across the screen it will intersect the x and y equations. The y coordinate is projected across and the x coordinate is projected across and up. The Lissajous figure is drawn where the project lines intersect.

Tracing a 3:1 Lissajous Curve x=cos(t)  y=cos(3t + $$$\pi$$$/2)
The phase shift is also very important to the shape. In the animation below you can see how the it changes as the phase shift is cycled across all possible values from 0 to $$$2\pi$$$.

Changing the phase relationship of a Lissajous Curve x=cos(t) y =cos(3t + $$$\delta$$$)
These curves aren't just a mathematical curiosity, they have a real world application. They used to be a very important tool for broadcast engineers. If two signals are feed into an oscilloscope (a tool that plots electrical waveforms) while it's in x-y mode, the Lissajous curve on the screen will reveal things about the signals.

The first thing to notice is that the number of horizontal and vertical lobes indicates the ratio of the frequencies. If the ratio of frequencies is rational (can be expressed as the ratio of two integers) the curve will be stationary. If not, it will slowly rotate like the second animation. If the two signal are meant to be locked together so that one is exactly 3 times the other like in the example above but the curve rotates, you know there's a problem. The rotation rate of the curve tells the engineer the deviation from the desired frequency. There are simple lookup tables like the one below that show what the curves should look like for a given phase shift and frequency ratio.


Frequency Ratios and Phase Differences

Earlier I said I'd elaborate on phase shift. The main thing to note is that you are working with two different frequencies.  1 degree of phase shift on one signal will take a different amount of time to 1 degree of phase shift on the other.  So it's important to not just note that there is a phase shift of 30 degrees, you have to specify what waveform you are referring to.  That's why a lot of the table results may be different from what you measure.  In a mathematical sense, it also makes a difference if you are talking about sine or cosine signal as one is a phase shifted version of the other.

Just to clear up another thing as well. The second animation is generated by plotting a waveform frame and then changing the phase an repeating this process. This is what makes it rotate. By chance though, this is exactly what you would see if the frequencies weren't locked together. A time varying phase is no different to making a small deviation to the frequency.
 
The code to generate the animations above is located here.  I did it as a quick project so didn't comment it.
 

Thursday, October 8, 2015

Generating 2 Level Harmonic Elimination PWM with Python

A couple years ago I wrote some code to generate Harmonic Elimination PWM (HEPWM) waveforms in octave, which is kind of like Matlab.  Recently someone asked if I could rewrite it in Python so it'd be easier to use and understand (fair call, it was pretty archaic).  I jumped at the chance as I'd been meaning to do it but just didn't have a strong reason to.  While I was at it I added a feature that may make it more useful, but more on that later.  To understand everything I recommend reading my previous HEPWM posts.

I should take a step back and first explain what HEPWM waveforms are.  If you're reading this I'm going to assume that you know what a PWM wave is and that you know the square nature of periodic PWM waves generate unwanted harmonics.  HEPWM is an extension of that idea, but the timing of the transitions in the wave are calculated to cancel or reduce specified harmonics.  The even harmonics are eliminated by default by making the wave half wave symmetric.  You don't have to make the wave quarter wave symmetric, that's a bit of a maths trick, but by doing that you halve the number of equations and unknowns that you need to solve for.  (If I remember correctly, solving the entire set of equations allows you to control the phase of the harmonics as well.)  That sounds really complicated, but the image below may help.

The upper waveform is typical of what you'd see on the output of an unfiltered inverter.  In this case the period of the wave is 20 ms, which means the fundamental frequency is 50 Hz.  The HEPWM calculations however have allowed the creation of a wave with the 3rd, 5th, 7th, and 9th harmonics set to zero and the fundamental frequency set to 0.6 (It looks like 0.3, but you have to remember that the positive and negative frequencies add in this case)  As we're controlling 5 harmonics (1, 3, 5, 7, 9) there needs to be 5 transitions in each quarter of the waveform.

Waveform
3 Level HE-PWM
The HEPWM waveform is constructed by starting with the base waveform below.  Multiple versions of this waveform are alternatively added and subtracted with the alpha values increasing each time (Alpha values are always between 0 and T/4)

Waveform
Quarter Wave Symmetric Waveform
Start with the definition for the Fourier series below.  It can then be applied to the above waveform.

Equation
Fourier Series
If you do the math X[0] = 0, B[k]=0 and A[k]=0 for even positive values of k.  For positive odd values of k, A[k] is shown below.

Equation
Fourier Series Coefficient for QWS waveform
This is the magnitude of a particular harmonic, k, for a particular value of alpha.  The total value for each harmonic can be found by adding all the components from each value of alpha (remember alternating alpha values cause the sign to change).  It helps to note the relation between the period and angular frequency.  Combining this with the above equation a system of equations can be generated and solved.  I go into more detail in previous posts, but this is needed to give context to the next addition.

Equation
Angular Frequency
The 3 level waveform in the first image is easy to generate with a H bridge topology, but what if you want to accomplish a similar effect with a microcontroller with only 2 output levels?  It seems that you can just shift the first half of the waveform up by adding the waveform below, but by doing that you are adding the Fourier series of the below wave to the Fourier series of the waveform calculated above.  This will invalidate the magnitudes of the harmonics you are trying to target.  I reused some images from a previous post that used a period of T but it makes more sense to use a generalised period of 2 pi.

Waveform
Translated Square Wave
All is not lost though, calculating the Fourier series of translated square wave above gives the result below.  There's nothing we can do about the DC offset, but the summation term can be easily added to the  calculated harmonics of the original waveform because it only has a sine term.  All we need to do is tweak the code the generates the set of simultaneous equations to be solved and we can now move to a 2 level system.

Equation
Fourier series of Translated Square Wave
 For clarity, a two level system with the 3rd, 5th, 7th, and 9th harmonics nulled and the main harmonic set to 0.3 is shown.  The main difference between the spectrum of this waveform and the one above is the DC offset.  That's what coupling capacitors are for.

Waveform
2 Level HEPWM
Below is the python code that does this little trick.  When the twoLevels variable is set to 0, the code runs as usual and generates the transition points for a 3 level waveform.  When it's set to 1, the adjustment term that describes the Fourier series of the square wave is included in the calculations.  (What the hell was I thinking with that comment "the location of the switching location")

Python Code
Calculating the Harmonic Magnitude in Python

Caveats, Warnings and Errata

This code isn't meant to hold your hand.  It's a demonstration and will probably have errors.  If you find any let me know.  It also assumes that your waveforms have perfect 0 time transitions with no overshoot or similar real world effects.  I recommend using it as a starting point.  Calculate your transition points and do a localised search with those values while running simulations closer to the real world.

I did my best to document things, but it's hard to comment symbolic math equations.  I may look into that.

Get the code!


Wednesday, November 26, 2014

Attempting to Determine How Audio Data is Stored in Flash Memory

You may have seen that I've recently been analysing a toy that plays animal sounds.  It's nothing complicated, it just plays one of 108 stored sounds when a collector card is scanned though an optical interface.  For some reason I thought it would be fun to see if I could replace the audio data with my own sounds.  From what I can tell, the data is stored in a 2 MiB flash memory IC on the PCB, I'm 99% sure it doesn't hold program memory.  2 MiB is way too much for such a simple task.  Besides, that amount of memory seems the perfect amount to store the sounds the player uses.

To examine the memory I'd have to get a programming adapter to download the contents of the chip.  I did a quick search of ebay and found the EZP2010 for $40.  I know what you're saying, "there are cheaper options available", and although that's true, this had a few things going for it.  It had a faster delivery, but most importantly it came with ZIF SOIC to DIL adapters, which turn a 30 minute job into a 30 second job.

Memory Programmer
EZP2010 Memory  Programmer
After stuffing around for an hour trying to set-up drivers, I finally got the programmer installed.  There isn't much to it, but it does the job.  It has the ability to copy chips as a standalone device not connected to a computer, but as I didn't need that I didn't bother testing it.

Memory Programmer
Device Under test in DIL ZIF socket
To read the memory of the IC it was removed from the PCB with a hot air gun and placed in the SOIC ZIF adapter.  Having these made the task so much easier.

Memory Programmer
Device Under Test in SOIC ZIF scoket to DIL adapter
There's a bit of confusion over what chip I'm actually trying to read.  The image below indicates that the Chip is a 25L1605D, but the programmer detects it as a 25L1635D.  Both have the same memory capacity and both give the same results when used as a setting to read the data form the memory.

Flash Memory IC
Flash Memory IC
Once you have the software set-up it's idiot proof.  Put the IC into the socket as shown in the diagram, detect device or configure it manually, then hit the read button.

HexDump
Flash Programmer Software
I tried playing the recovered data as audio by importing it as different types of raw data in Audacity and Goldwave, but each time all I got was static.  It would've been unlikely to get the exact format, but I was hoping for some type of recognisable distortion that would help to reveal the nature of the data to me.  No such luck.

I expect to see an area to tell the device how many sounds are in memory and something like a lookup table to indicate the location of each sound byte.

My goal is to see if I can determine the structure of the data, and as a first quick test I checked out the data using the histogram function of HxD.  As you can see from the image below, apart from the spike in the centre, all the bytes seem to be evenly distributed.  Not what I was wanting to see.  It's not a certainty, but If you see an even distribution of bytes it indicates encryption or compression has been used.  I was a little excited to see the spike in the middle though.

Histogram
Byte Histogram of recovered Data
That excitement was short lived.  It turns out that there is a large block of unused memory at the end of the file containing the character 0x80

HexDump
Repeated 0x80 at end of file
To get a better idea of what I'm looking for, I had a look through digikey to find a sound playing IC that could be similar to what's used in this toy.  There's no way to know what device has been used as it's a chip on board device, but chip manufactures like to compete on features, and if it's in one companies IC there's a good chance that it's in the others too.

The cheapest device I found was a ISD3800 chip corder and a quick look at the data sheet gives us some important insights.  It supports the type of memory that our toy uses, and shows some of the audio compression algorithms that could be used.  The algorithms used may not observe byte boundaries i.e. 2,3,4,5,6,7,8, 10, 12 bit samples.

DataSheet
ISD3800 sound player IC data
For more analysis the data was opened in Audacity with the spectrograph view turned on.  There are three distinct features visible here.  Two vertical lines and a gap at the end.

Spectrogram
Spectrogram of Data when opened as a raw audio file in Audacity
Zooming in on the waveform at the first vertical line shows a couple of triangular shaped waveforms.

WaveForm
Interesting Section of Data in Audacity
The second vertical line indicates this descending step feature in the waveform.

WaveForm
Interesting Section of Data in Audacity
As seen before, the section at the end of the file is a grouping of the 0x80 byte, in this format interpreted as a zero.

WaveForm
Nothing at end of File
I follow +Oona Räisänen on twitter and have seen how useful baudline can be.  So I gave it a try.  I'm still learning the interface, but it will come in handy for a few other tests I want to run.  While in Linux I tried the data in binwalk but got absolutely nothing.  The entropy plot did show the regions noted above though.

Spectrogram
Baudline Interface
The bit view window turned out to be not so helpful,  the poincare plot was all black.  I'm not sure if I used it right, did I over saturate it and it just shows everything  as black.  I might do my own in octave.

Binary Data
Statistical Analysis of Data in Baudline
So where am I at?  I have more of an idea of what I'm looking for, but have no leads.  I have an SPI bus protocol analyser coming that will come in handy.  I can play one sound and record what memory addresses it accesses and what data is returned, for some reason they may not use a sequential addressing system. The rate it does this could also reveal that a variable bit rate compression algorithm was used.

To sum up, I don't like my chances, but it's a fun cat and mouse game.  I'm learning some new techniques, while solving a challenging puzzle.













Saturday, November 15, 2014

How a Collector Card Sound Player Works

Today I'm going to have a bit of fun.  Once again the local supermarket has released a set of cards with pictures of animals on them for children to collect.  I've written about certain mathematical aspects of these promotions before.


This time they've added an extra feature to the cards, when swiped through an electronic device, that's also for sale, the sound of the animal on the card is played back for the collector.  In this post I'll give a quick explanation of how the device works.

The cards come in a sealed pack of 4 so people can't pick and choose what ones they'll get, it's a random draw.  They do however have a bar code printed on the back that allows the sound player to know what card it is, and what corresponding animal sound to play.

Collector Cards
Sealed Animal Collector Card Pack
The device that reads the barcode from the card and plays the appropriate sound is a simple mass produced device that seems to be rugged and reasonably well built for a six dollar product.

Card Reader
Card Reader
I posted a rather quick teardown of the reader as soon as I bought it.  It's not very detailed but it gives you an idea of how It works, and will make the rest of the details make more sense.


The internal construction is what you'd expect from a high volume low price device.  I estimate that they probably had somewhere around 100,000 of these readers made, so it's no surprise to see a chip on board solution on a single sided PCB of medium quality.  It comes with three no name AAA batteries.  I haven't probed the board, but I can't see any voltage regulation. There's a bulk capacitor on the input and there could be a regulator under the black blob.

Electronics
Card Reader Construction
The barcode on the card is sensed by an optical reader that's located on a separate board over the swiping slot.  It's held in place by plastic studs that are melted once it's installed.  The reader seems to consist of an infra red LED and an optical sensor like a photo-diode.  As the card is drawn through the slot under the reader, the amount of light reflected from the barcode changes and can be detected.  This is how the barcode is read and the reader know what sound to play.

Card Reader
Reader Slot

PCB
Active Optical Sensor
The part I'm most interested in is the 8 pin SOIC package on the board.  It appears to be a Macronix MX25L1605D 16 Mib flash memory IC and is the most likely place to store the sounds played by the device.   I'm curious as to how the data is stored on it.  There are 108 cards and each card seems to have a unique sound, this means that there are 155 kb for each sound. If we assume a worst case scenario of 4 bit audio (It's a pretty average speaker, any more bits and you'd be wasting them), that means there are about 38 k samples per audio clip.  Clips last around 4 seconds so that would mean about 10 k samples per second.  Due to Nyquist, this would limit the maximum frequency to 5 kHz.  This is a possibility, but I'd really like to get the data off the chip and see for myself.  While I'm there I'd also like to know if I could reprogram it and maybe put different audio on it.  This post was originally going to be an attempt to do that, but the flash memory reader I ordered hasn't arrived yet.  Oh well.

PCB
Sound Card Reader/Player Circuit Board
In the meantime, if you want to impress your friends (NOTE:  if you think this will impress your friends you probably don't have any) you can show them how to read the barcodes on the back of the card to identify the number inside.

The barcode is simple binary and is made up of 13 bits.  Each bit is made of a black bar and a white bar.  If the bit is a one, the symbol will be a black bar 2 units wide followed by a white bar 1 unit wide.  If the bit is a zero, the symbol will be a black bar 1 unit wide followed by a white bar 2 units wide.  To put it simply, thin black bar = 0, wide black bar = 1.

After looking at a few cards it was obvious the last bit was always one.  Those of you familiar with serial communication will be comfortable with me calling it a stop bit.  I initially couldn't tell what the next two bits were, but it became clear that the rest of the code was the number of the card in binary.  Once again drawing on my experience with serial communication I assumed that the two bits I didn't understand were parity bits.  A pattern started to emerge, if there were an even number of wide black bars in the code these bits would be 01, if there were an odd number of wide black bars, the parity bits would be 10.

As some of the people reading this may not understand how to read binary, I've demonstrated in the image below how to easily read the code.  Starting at the bar 4th from the right, put the number 1 under it.  Under the bar 5th from the right, put the number 2, continue this pattern, doubling the number each time. i.e. 1, 2, 4, 8, 16, 32, 64, 128, 256, 1024. Once this is done you add the numbers under the wide bars together to get the number of the card.

Notes
Decoding the bar code on the cards
TADA!  Card number 108.

I've also included an ODS Spreadsheet you can use to generate your own barcodes.  Just change the number in the left column and the barcode will automatically generate for that number.

Animal Card
Animal Card

Saturday, October 26, 2013

Harmonic Elimination PWM Comparison and Uses

I've been doing a series about how to calculate the switching times of harmonic elimination PWM waveforms and I thought it was time to compare HEPWM to another method and look at how it can be used.  To catch up on the theory so far, have a look at the rest of the series.


All the code for this post can be found here.

A simple well known method for generating PWM waveforms is to compare a sawtooth wave to the desired signal and set the value of the PWM waveform high whenever the signal is higher than the sawtooth wave.  It gives reasonably good results, but I'd like to know how it compares to the HEPWM method when it comes to controlling harmonics.  When you're switching power loads there will be switching losses.  Ideally you want to minimise the amount of switching you do, but it comes with the price of not being able to control harmonic distortion as well.  It's one of those engineering trade-offs you have to make based on your design.  So to be fair and compare apples with apples, both the PWM and HEPWM waveforms below each have 40 switching transitions per cycle.  This means that switching losses should be equal and we can compare them using other metrics.


Waveform
A half cycle of the PWM waveform is generated as mentioned above.  This and an inverted copy of it are concatenated to produce a full wave.
PWM Waveform
Although the generated waveform is inverted (my bad) it's hard to tell it apart from the HEPWM wave below.  Upon closer inspection you can see that the switching times are different, but on first glance everything looks the same.
PWM Waveform
Below is the real test.  The HEPWM waveform knocks out the harmonics right up to the 20th harmonic, whereas the basic PWM signal only kills the harmonics up to the 10th before they start creeping up.  This may be fine for what you're doing, but if you absolutely need to control certain harmonics, HEPWM is the way to go.  It might mean that your output filter is cheaper, or lighter, or takes up less space.  If you need to control a specific harmonic, HEPWM can do it.  It may be an issue with EMC you have to take care of.  If for some reason the geometry of the enclosure you're using is letting a certain frequency through, taking care of that in software rather than adding more shielding is going to save yourself a headache.
FFT of a PWM Waveform
FFT of a PWM Waveform
HEPWM also allows you to easily control the magnitude of the output while controlling harmonics.  If you're making a power inverter, ideally you want to run at nearly full magnitude to get the most out of your design, but you can still trim the output if you need to.  The graph below shows how you can pre-compute switching time for different magnitudes.  While running, the magnitude can be changed by selecting the set of switching times for the desired output magnitude.
Switching Times
HEPWM has some disadvantages, it's only useful in situations where you can pre-calculate the waveform.  It's not going to work with a signal like audio.  It's suited to power inverters and other niche applications where you want to reduce harmonics of a simple waveform like 50/60 Hz mains power.  It may not be for everything, but when the situation does call for it you've now got the right tool for the job.

To learn more I encourage you to read this thesis by Yu Yang.  It gives an overview of some other switching techniques and goes into a little more depth on some of the details.

Monday, September 30, 2013

Octave Code For Generating Harmonic Elimination PWM Waveforms

I've finally sorted out the code to generate PWM waveforms that you'd use to control an electronic device such as an H-bridge driver.  I must warn you this is engineering coding, error checking is non existent, and things may be a little rough around the edges, but it'll get you started on a version for your specific problem.  It should also be noted that some problems can't be solved.  If the magnitudes are set too high you could end up in a situation where there is more power in the waves spectrum than can be in the actual waveform.  In that case the solver will do its best to minimise the objective function but it won't actually find a solution.

The code will generate a waveform that you can scale to the desired frequency.  By entering two vectors, one containing the harmonics of the output waveform that you want to control, and the second containing the magnitude of the harmonics, a set of switching times will be returned.  It's also important to remember that all even harmonics are automatically zeroed due to the quarter-wave symmetry of the waveform.

As a demonstration I've generated the example waveform below that sets the first harmonic magnitude to 0.5 and all odd harmonics up to the 31st to zero.

The code for this demonstration can be found here.
HE PWM Waveform
HEPWM Waveform
The FFT of the above waveform is shown below.
HE PWM Waveform
FFT of a HEPWM Waveform
Although the magnitude of the first harmonic is set to 0.5 the FFT shows two peaks of 0.25.  This is due to the nature of the FFT showing positive and negative frequency.  These combine to give the 0.5 magnitude.  All the harmonics up to the 31st are however zeroed.

There are still a couple aspects of this method I'd like to investigate, but all the heavy lifting is done and we can get into some practical aspects of the process.


Thursday, September 19, 2013

Fourier Series of Harmonic Elimination PWM Waveforms

Harmonic elimination pulse width modulation waveforms can be used to control or completely dampen specific harmonics in a switched signal, and in my last post I laid the groundwork for calculating the Fourier series of these waveforms.  By making the waveforms quarter-wave symmetric the maths becomes a lot easier by eliminating all even harmonics and giving a set of equations easier to solve.  Half-wave symmetry should also remove even harmonics and give a more versatile, efficient waveform, but for the following exploration of the topic I'll only be focussing on quarter-wave symmetric waveforms.

Below I show how combining simple waveforms from my first post with different alpha values can generate the desired waveform.  Its paramters can be found by solving a set of non linear constrained simultaneous equations.  Solving these is not trivial and requires the use of software like Matlab or Octave.  I am currently working on porting some Matlab code that I wrote several years ago for this specific purpose to Octave.  I would have liked to have it ready for this post but I've had a few things to take care of lately and I'd prefer not to rush it.  It should be ready for my next post though.

You can get the PDF of the work below here.
Harmonic Elimination PWM Equations

Harmonic Elimination PWM Equations

Harmonic Elimination PWM Equations

Sunday, September 8, 2013

Fourier Series of a Quarter-Wave Symmetric Pulsed Waveform

I'm looking into Harmonic Elimination Pulse Width Modulation (HEPWM) and it requires a bit of maths.  So although there isn't anything too exciting in this post, it's laying the groundwork for some future content.

HEPWM is a technique to control the magnitude of harmonics in a PWM waveform.  It takes the basic PWM process that most people are familiar with and tweaks it to allow control of the magnitude of harmonics in the generated waveform.  It's used in a couple of places, but it's biggest use is calculating switching waveforms in DC to AC inverters.  By switching a DC voltage in the correct sequence, you can push the harmonic distortion in the switched waveform into higher frequency harmonics.  This doesn't seem too useful at first but it allows the use of smaller passive filters on the output to remove distortion.  As the distortion is in higher frequency harmonics, the filters can be more basic with a gentle roll-off.

One of the methods used to generate HEPWM waveforms is based upon Quarter-Wave Symmetric (QWS) Pulsed Waveforms.  The Fourier series of the waveform is needed to do further calculations, so I've started at that point.  Although I've only calculated the Fourier series for a very basic QWS Pulsed Waveform, the Fourier series of any QWS waveform can be generated by summing scaled versions of the basic Fourier series I've calculated with different parameters.

You can get the pdf here.
Quarter-Wave Symmetric Waveform
Quarter-Wave Symmetric Waveform
Quarter-Wave Symmetric Waveform