Lab 08 - Jay1337/computational_physics GitHub Wiki

Goal

The goal of this lab was to develop an understanding of the fourier transform. We also wanted to be able to practice using np.fft.rfft, np.fft.rfftfreq, and np.fft.irfft to compute, interpret, and reconstruct signals, so that we could analyze and plot data and audio recordings.

Overview

This lab was predicated on the fourier transform so we wanted to develop some fourier transforms using the sum of cosine and sin functions. Doing so we wanted to get the fourier transform of the summed wave to then interpret the results. In this lab we about understanding the fourier transform through summing three cosine functions then interpreting its frequency components via FFT and inverse FFT. This lab allowed us to illustrate how different frequency components and phase relationships combine in the time domain, as well as demonstrating spectral decomposition, peak identification, and lossless reconstruction. We were also able to see how fourier spectra reveal timbre and instrument identity, as well as pitch detection. Lastly we used our understandings to build reproducible scripts for data loading, plotting, spectral analysis, and noise filtering based off what we had done previously in the lab.

Part 1

In the first part of this lab we needed to sum the cosine functions so that we could then use them in our fourier transform. In order to do so we needed to create a time array t from 0 to 4 s with 100 points, then define three cosines with amplitudes A = [0.5, 1.0, 0.75], angular frequencies ω = [π, 2π, 4π], and phase shifts ϕ = [π/2, π, 3π/2]. image image

Part 2

In our second part we computed the fourier transform based off a summed waves, and that was done so by using np.fft.rfft which returns the amplitudes of the fourier transform components of the input data, giving component frequencies that make up the input function, as well as np.fft.rfftfreq which calculates frequency array that corresponds to the fourier transform amplitudes, and np.fft.irfft which returns the inverse fourier transform, converting frequency data back to time data. Through this we could find our power then plot power vs frequency which allows us to identify which frequencies are most prominent in a signal. image image

Part 3

In this part we analyzed our sunspots data and we did so by downloading sunspots.txt, and loading columns into month and nspots. After that was done we could move onto plotting nspots vs. month which visually estimating a 10–12 year cycle. After applying FFT to nspots, which computed power vs. period we could identify a dominant peak which occurred at around 11 years. This confirmed the validity in our fourier transform calculations. image image

Part 4

For part 4 we built a square wave by summing sine waves of various frequencies and amplitudes. image image

Part 5

This last part focused on analyzing the sound of musical instruments. By downloading and reading the files of instruments into our code as well as loading them into arrays we could compute FFTs, power spectra, and frequency arrays at 44,100 Hz sampling. This computation allowed us to plot power vs frequency side by side where found that both instruments share the same fundamental frequency peak, where the trumpet has clear strong evenly spaced harmonics, and the piano has richer rapidly decaying series of overtones. This also allowed us to confirm pitch and how it is indeed in audible range. image image

Part 6

Lastly in the second part of our lab we used what we learned in the first part and applied it by making our own code that give us the fourier transform and not only read, but also plot the results for the input signal and fft spectrum for four different instruments.