Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Old MathJax webview please I need proper python code for this question please is this question from computational physics exercise 7.7 by mark newmann FURTHER
Old MathJax webview
please I need proper python code for this question please is this question from computational physics exercise 7.7 by mark newmann
FURTHER EXERCISES 7.7 Fast Fourier transform: Write your own program to compute the fast Fourier transform for the case where N is a power of two, based on the formulas given in Section 7.4.1. As a test of your program, use it to calculate the Fourier transform of the data in the file pitch.txt, which can be found in the on-line resources. A plot of the data is shown in Fig. 7.3. You should be able to duplicate the results for the Fourier transform shown in Fig. 7.4. This exercise is quite tricky. You have to calculate the coefficients Emi) from Eq. (7.43) for all levels m, which means that first you will have to plan how the coefficients will be stored. Since, as we have seen, there are exactly N of them at every level, one way to do it would be to create a two-dimensional complex array of size N X (1+ log, N), so that it has N complex numbers for each level from zero to log, N. Then within level m you have 2" individual transforms denoted by j = 0...2 1, each with N/2" coefficients indexed by k. A simple way to arrange the coefficients would be to put all the k = 0 coefficients in a block one after another, then all the k = 1 coefficients, and so forth. Then Em.) would be stored in the j +2"k element of the array. -(,i This method has the advantage of being quite simple to program, but the disad- vantage of using up a lot of memory space. The array contains N log, N complex - a numbers, and a complex number typically takes sixteen bytes of memory to store. So if you had to do a large Fourier transform of, say, N = 10% numbers, it would take 16N log, N-42 gigabytes of memory, which is much more than most computers have. An alternative approach is to notice that we do not really need to store all of the coefficients. At any one point in the calculation we only need the coefficients at the current level and the previous level (from which the current level is calculated). If one is clever one can write a program that uses only two arrays, one for the current level and one for the previous level, each consisting of N complex numbers. Then our transform of 10% numbers would require less than four gigabytes, which is fine on most computers. (There is a third way of storing the coefficients that is even more efficient. If you store the coefficients in the correct order, then you can arrange things so that every time you compute a coefficient for the next level, it gets stored in the same place as the old coefficient from the previous level from which it was calculated, and which you no longer need. With this way of doing things you only need one array of N complex numbers--we say the transform is done "in place." Unfortunately, this in- place Fourier transform is much harder to work out and harder to program. If you are feeling particularly ambitious you might want to give it a try, but it's not for the faint-hearted.) of diffrac- 11 FORMULAS FOR THE FFT Let us look at the mathematics of the fast Fourier transform in a little more detail and consider the various stages of the decomposition process. In true Python fashion we'll number the initial stage "stage zero". At this stage we have a single Fourier transform of the entire set of samples. At the next stage, stage 1, we split the samples into two sets, at the stage after that we split again into four sets, and so forth. In general, at the mth stage of the process there will be 2 sets consisting of N/2" samples each. The first of these sets consists of the original samples numbered 0,2", 2" x 2.2" x 3 and so forth. In other words it consists of sample numbers 2" with integer r = 0...N/21 - 1. Sim- FURTHER EXERCISES 7.7 Fast Fourier transform: Write your own program to compute the fast Fourier transform for the case where N is a power of two, based on the formulas given in Section 7.4.1. As a test of your program, use it to calculate the Fourier transform of the data in the file pitch.txt, which can be found in the on-line resources. A plot of the data is shown in Fig. 7.3. You should be able to duplicate the results for the Fourier transform shown in Fig. 7.4. This exercise is quite tricky. You have to calculate the coefficients Emi) from Eq. (7.43) for all levels m, which means that first you will have to plan how the coefficients will be stored. Since, as we have seen, there are exactly N of them at every level, one way to do it would be to create a two-dimensional complex array of size N X (1+ log, N), so that it has N complex numbers for each level from zero to log, N. Then within level m you have 2" individual transforms denoted by j = 0...2 1, each with N/2" coefficients indexed by k. A simple way to arrange the coefficients would be to put all the k = 0 coefficients in a block one after another, then all the k = 1 coefficients, and so forth. Then Em.) would be stored in the j +2"k element of the array. -(,i This method has the advantage of being quite simple to program, but the disad- vantage of using up a lot of memory space. The array contains N log, N complex - a numbers, and a complex number typically takes sixteen bytes of memory to store. So if you had to do a large Fourier transform of, say, N = 10% numbers, it would take 16N log, N-42 gigabytes of memory, which is much more than most computers have. An alternative approach is to notice that we do not really need to store all of the coefficients. At any one point in the calculation we only need the coefficients at the current level and the previous level (from which the current level is calculated). If one is clever one can write a program that uses only two arrays, one for the current level and one for the previous level, each consisting of N complex numbers. Then our transform of 10% numbers would require less than four gigabytes, which is fine on most computers. (There is a third way of storing the coefficients that is even more efficient. If you store the coefficients in the correct order, then you can arrange things so that every time you compute a coefficient for the next level, it gets stored in the same place as the old coefficient from the previous level from which it was calculated, and which you no longer need. With this way of doing things you only need one array of N complex numbers--we say the transform is done "in place." Unfortunately, this in- place Fourier transform is much harder to work out and harder to program. If you are feeling particularly ambitious you might want to give it a try, but it's not for the faint-hearted.) of diffrac- 11 FORMULAS FOR THE FFT Let us look at the mathematics of the fast Fourier transform in a little more detail and consider the various stages of the decomposition process. In true Python fashion we'll number the initial stage "stage zero". At this stage we have a single Fourier transform of the entire set of samples. At the next stage, stage 1, we split the samples into two sets, at the stage after that we split again into four sets, and so forth. In general, at the mth stage of the process there will be 2 sets consisting of N/2" samples each. The first of these sets consists of the original samples numbered 0,2", 2" x 2.2" x 3 and so forth. In other words it consists of sample numbers 2" with integer r = 0...N/21 - 1. Sim
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started