A sample text widget

Etiam pulvinar consectetur dolor sed malesuada. Ut convallis euismod dolor nec pretium. Nunc ut tristique massa.

Nam sodales mi vitae dolor ullamcorper et vulputate enim accumsan. Morbi orci magna, tincidunt vitae molestie nec, molestie at mi. Nulla nulla lorem, suscipit in posuere in, interdum non magna.

Finding the frequency components of a Qucs Output.

Initializing the Python Fourier Transform Program

Fig 1: Initializing the Python Fourier Transform Program

Tonight’s post is again about the harmonic frequencies present in waveforms that are not simple sine waves. I needed a way to analyse the outputs of the circuits we were simulating in Qucs. To do that I had to create myself a tool. I wrote a fairly simple program to perform the Fourier Transform on those waveforms. I show the complete process in the embedded video, but I need to add a whole lot more detail about the actual program.  To do that it is necessary to go through the code line by line.

When I write a program for my use I normally write it for use with a command line.   Writing all the things necessary to make a pretty window is something I do not enjoy and it is easier just to do it in text format. Besides, I really don’t need the point and click ease of operating my own program.

In Figure 1 I show the initialization of the file names.  There is a difference between the way Linux and Windows divide the path name into various folders so there is a section with the Windows name and the Linux name.  Obviously if someone wants to use this program they will have to modify the path name to the directory where they place the data.  I will be discussing each of the individual file names as we go through the program.  After I initialize the file names, the next step is to initialize the parameters for the data to be read in from the CSV file from Qucs.  This can probably be done directly from the CSV but for my use it was simpler jut to input it here by inputting the sample time used and the base frequency.   I always used 1 HZ although  most of the signals used in the Qucs simulation were 500 Hz.  It really doesn’t matter since what we are interested is the harmonic number and not the actual frequency.   The thing that is important is that we do one and only one complete cycle in our Qucs simulation and the output waveform that has been converted to a CSV file.

Fig 2 Reading the Qucs CSV file.

Fig 2 Reading the Qucs CSV file.

The next step is shown in Figure 2.  In this step. the CSV file is read.  The CSV output from Qucs consists of many rows of 3 columns of data.  The first row consists of text and is the names of the columns.  I skip over that row.  Then, I simply split each of the remaining rows into 3 text strings by splitting based upon the semi-colon separator Qucs uses.   These strings are then turned  converted into numbers and each one is appended to a list.  I call the lists c1,c2, & c3 for column 1, column 2,  column 3.

Once all the data is read in and put in the lists, the file is closed.   The next 4 lines, the print statements, were used during the debugging of the program to make sure I was reading the correct number of points and that this number matched my calculation.

After that I create another CSV file with the ending test appended to the file name.  It was used while I was debugging and when I was running the fourier transform on a simple sine wave so part of the output compares the input (column 2) to a calculated sine wave.  Now that I have the program running I need to add comment marks to that code so it does not actually consume time or disk space by creating this file.  Once I write some code like this I like to keep it around just in case I may need it again.

Fig 3:  The Fourier Transform.

Fig 3: The Fourier Transform.

Figure 3 shows the code where the work is really done. Four empty lists are initialized that will contain the coefficients for the harmonic values. Coefficient is the multiplier for the harmonic wavefore or basically a strength value. The rest of this section of code is two nested loops. The outer loop is the harmonic number and the inner loop is the sample number of the data points from the Qucs output csv file. Harmonic number 0 is the DC offset and it is simply an average taken by summing all the data points and then dividing by the number of samples. All of the other harmonic coefficients are calculated by taking the average of multiplying the sample value by a sine or cosine of the harmonic that is calculated for the same time.

Once the average for each harmonic is calculated the value is appended to the correct list and the harmonic number is incremented and the process is repeated until the harmonic number reaches the maximum number we are interested in.   In the case of the example used in the video the inner loop ran 10001 times for each of the outer loops from 0 to 1000 harmonics. That is a lot of calculations!

Fig 4: Create and write the coefficient CSV file.

Fig 4: Create and write the coefficient CSV file.

Now that we have calculated all of the harmonic  coefficients we need to write those to a file so these can be imported into Veusz and graphed. This is shown in Figure 4 and is simply a loop writing each of the list values calculated in the Fourier Transform shown in Figure 3.

Now if I have done everything right, I should be able to take all those correctly sine and cosine waves and add them together and create the wave we started with.    Eventually I did everything right, but there was some moments while writing and debugging I was beginning to wonder if I was going to get it done!

Figure 5 Building the waveform by summing the harmnic waves.

Figure 5 Building the waveform by summing the harmnic waves.

To rebuild the wave I again used two nested loops. First I created several empty lists. The outer loop in this case was the number of sample points and the inner loop was the harmonic number. For each sample time I calculated the value each harmonic wave would create and added all of these together. The summation was appended to the list and then on to the next sample point and do it all over again.

I have not talked about it, when doing the Fourier Transform and now when doing rebuild, I also calculated the absolute value coefficient taking the square root of the sum of the square of the sine and cosine coefficients and the phase shift caused by doing this.  (See my previous post. Why both the sine and cosine waves. for an explanation.)    Now that I am rebuilding the wave I am also rebuilding one using these two numbers.

Figure 6 writing the rebuild CSV file.

Figure 6 writing the rebuild CSV file.

Finally Figure 6 shows the writing of the rebuilt waveform into a CSV file with the name rebuild appended to the file name.   This CSV file also includes the original file as well as two residual files showing the difference between the rebuilt waveforms and the original waveform.  (I acually have never viewed that part.  I was happy enough just viewing the waveforms using Veusz.)

Veusz is a graphing program I used to import the CSV files.  I describe it in several posts and videos.   The first one is called: “Veusz How I create all these graphs Part 1”  Python was explained back when I was still trying to do this with podcasts.  However, the related posts do have some very good links.  Look at “Episode 44 Programming to learn Logical Thinking” and “Episode 45 – More About Programming and Python”  The circuit simulation program Qucs has been mentioned a lot lately but you can find all the posts related to it by using the search function on this site and search for Qucs.

This has been the hardest and most complicated post yet.   If you have made it this far congratulations!  Pat yourself on the back.  The next couple posts will be a lot easier.   We are going to look at the results of what we have done and also look at some other diverse things the Fourier Series comes into usage besides just sound and music.

If you have questions or concerns please comment on this post or e-mail me.  Also if you want a copy of this python program, e-mail me and I will send it to you.

Gary

The video associated with this post is embedded below:

 

Creative Commons License
Finding the frequency components of a Qucs Output.r” by Create-and-Make.com is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

If you found this post to be enjoyable and interesting please consider subscribing to this blog using one of the methods on the home page or the e-mail subscription form also found there and at the bottom of each page.

And if you enjoy this please share this site with your friends.


 

Print Friendly

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>