Project Files
Introduction and Objectives
The motivation for this project came from a recognition in class that many of the filter designs we were looking at had a resemblance to the probability distributions I had been working with in my statistical mechanics class, PHYS 362. These probability distributions were typically studied in the context of a variety of steady-state, analytic 'particles-in-a-box' scenarios, in which probability distributions of parameters such as speed or energy would change shape and form depending on the box parameters. Thus, it seemed as though it could be possible to make a statistical mechanics-inspired equalizer, mapping the analytic probability distribution of a certain quantity to the frequency response of a digital filter. Fortunately, there was a wide breadth of these 'particles-in-a-box' scenarios that we studied, and thus several distinct filter could be designed (Maxwell-Boltzmann, Fermi-Dirac, and Bose-Einstein distributions).
Thus, the objectives of this project were to design filters for each of these distinct scenarios!
Procedure and Theory
General Configuration
At the top of the script, the user is able to control several audio and environmental parameters.
In regards to the audio parameters, the user has control over the sample rate, finite impulse response length (number of taps, must be odd), the type of window (Hamming, Hanning, or Blackman), the size of the frequency-sampling grid, the anchor frequency for the relative mapping mode, the clipping threshold, and the lost probability threshold.
In terms of environmental parameters, the user first switches between the classical or quantum regime.
In the classical regime, they can control the dimensionality of the system (one-, two-, or three-dimensional), the box lengths in meters, the temperature of the environment (Kelvin), the boundary condition (hard or periodic), the frequency mapping method (relative or absolute), and the scale factor to use in absolute mode (audio Hz per transit Hz). Then, the user is able to list the species types and numbers from a pre-built lookup table or as custom entries for the classical regime (custom:amu).
In the quantum regime, users can similarly control the diemsnionality of the system, the box size (in meters, but typically in this regime nanometers is recommended), the temperature (Kelvin), the boundary condition (again, hard or periodic), the frequency mapping (relative or absolute), and the scale factor in absolute mode (audio Hz per Joule). In the quantum regime, the simulation only allows for one particle species in the environment, which is to say no species mixing . The user then selects the particle species from the following: Fermions — electron, proton, neutron, He-3, Li-6, K-40, or Massive bosons — He-4, Li-7, Na-23, K-39, Rb-87. After this selection, the user is able to choose the number of particles, and the thermal energy cutoff (used for help with numerical integration).
Regime Differentiation
The following sections are distinguished in their regime by several key assumptions. Firstly, a classical gas is well approximated by Maxwell-Boltzmann statistics in the scenario in which the thermal De Broglie wavelength, given by
is much less than the average spacing between particles. This collapses at very low temperatures, in which the thermal wavelength becomes very large. The classical gas approximation also requires low densities, given by the condition
At very high densities, which can occur at low temperatures or very small box dimensions, the gas becomes degenerate, and begins to obey quantum statistics instead of classical Maxwell-Boltzmann. Within the script, there are safeguards to ensure that these limits are adhered to, and warnings for the user if their input parameters are no longer classical.
It is also important to note that the classical regime is considered in the non-relativistic limit, which is such that:
Classical Regime
Due to equipartition dominating the nature of energy distributions in the classical regime, with all distributions proportional to temperature up to a scale factor determined by dimensionality, the classical regime distribution is thus the probability distribution of particle speeds. In the classical regime, energy level spacings within the box are small enough such that the the gas can be described by the Maxwell-Boltzmann weight \(e^{-mv^2/2kT}\) times the number of velocity states within a shell of radius \(v\), which scales as \(v^{d-1}\). For one species in \(d = 1, 2\) or \(3\) dimensions this gives
where \(\Gamma(x)\) is the Gamma function. The mean speed is
Speed is converted to frequency by the rate at which it traverses the box:
where \(\tau = 2\) for hard walls (a round trip) and \(\tau = 1\) for periodic walls. A mixture is the number-weighted sum
in which s indicates species. In relative mode the audio scale is
so the mean transit frequency lands on \(f_{\text{anchor}}\).
In the relative mode, temperature has no impact on the shape of this distribution, and the 'smearing' effect of the quantity is overwritten by the selection of the anchor frequency.
However, in absolute mode \(\kappa\) is user-set, so \(T\), \(m\) and \(L\) all change the filter shape, and the density on the audio axis carries the Jacobian,
Quantum Regime
In a small box the allowed momenta are discrete,
and the kinetic energy of each level is relativistic,
Each level is filled according to
using + for fermions (Fermi-Dirac) and − for massive bosons (Bose-Einstein). For fermions and massive bosons the chemical potential \(\mu\) is solved numerically such that the number of state occupations add up to the particle number,
where \(g\) is the spin degeneracy from the lookup table and \(\rho\) is the continuum density of states. Each level then carries a probabilistic weight and therefore the distribution has mean energy
Relative mode maps \(f_\text{audio} = \frac{f_\text{anchor}E}{\langle E\rangle}\), and absolute mode maps \(f_\text{audio} = \frac{\kappa\,E}{h}\) with user-set \(\kappa\). A cold Fermi gas has a sharp edge at \(E_F\), and at \(T = 0\) its mean energy is
Anchoring \(\langle E\rangle\) therefore puts the lowpass cutoff at \(\dfrac{d+2}{d}\) times the anchor frequency, in the non-relativistic limit.
It is also important to note that the ground level energy state is always zero.
Finite Impulse Response Filter Design
To design the finite impulse response filter, the obtained frequency spectrum is mirrored and appended to account for negative frequencies.
Then, the inverse Fourier transform is taken to obtain an impulse response centred about zero. This impulse response is then windowed according to the user-set window type, and then the input signal is filtered based on the determined filter coefficients.
Challenges
There were several iterations of this project over the course of the work. Initially, I considered the basis of the filter parameters to come from molecular dynamics, however this would be heavily computationally intensive and seemed as though it wouldn't work for live input in any possible scenario. The analytic solutions to these problems were also what I was more familiar with, and so it seemed more appropriate to simply consider those solutions as the basis instead.
After determining this, and beginning work on the project, there were several other hurdles to overcome. Another early conceptualization of implementation had a built-in understanding of the quantum/classical limits, and discrete/continuum limits, and would allow for the mixing of these systems. However, this proved to be very, very complicated. Particularly considering the dominance of some statistics over others in these scenarios (electron in a 3D quantum box has a very different energy scale than \(10^6\) xenon atoms in a 3cm x 3cm 2D box).
So, to handle to classical/quantum limit case, it seemed as though the most appropriate course of action was to separate the two regimes and warn the user if their input parameters in the classical regime appeared to fit better with the quantum regime.
In regards to the discrete vs. continuum limits, I had early concerns that the implementation of discrete limits would look like a comb filter with very narrow spacing in the frequency domain. After several attempts to design the environment in the discrete limit, I realized that a comb filter isn't really the kind of filter I wanted, and rather I wanted the shape of the quantum filters to be that of their continuum limits.
This also had some additional added benefits. Without the consideration for discrete states, all of the distributions were smooth, and thus could be easily dealt with by the filter design function!
Regarding the filter design function, I tried several methods, particularly with the use of invfreqz.
Examples
Below is a sample filter design and resultant output spectrum for a test input signal. The filter considers the following parameters at a sampling rate of 44.1kHz:
- Quantum regime
- Two-dimensional
- Temperature: 0.1K
- Box dimensions: [10 10]*1e-9
- Relative mapping: 4400Hz anchor
- Particle species and count: 2500 protons
Below is another filter design and resultant output spectrum for a test input signal. The filter considers the following parameters at a sampling rate of 44.1kHz:
- Classical regime
- Three-dimensional
- Temperature: 900K
- Box dimensions: [0.3 0.3 0.3]
- Relative mapping: 4400Hz anchor
- Particle species and count:
- 2e24 Helium atoms
- 1e24 N2 atoms
- 3e23 O2 atoms
- 6e21 Argon atoms
- 3e20 CO2 atoms
Function-by-Function Breakdown
The following section will discuss the implementation, method, and physics behind a few of the functions used. Several simpler functions, like the lookup table functions, will be omitted from this discussion for brevity. They can be scrutinized in detail in the code, if desired!
Classical Regime
In order to input the classical regime scenario into designLinearPhaseFir, we must obtain probDensityPerHz. The following function is for this purpose!
buildClassicalDensity
The first two code blocks in the function grab all of the necessary, general systems configuration values, as well as grabbing all of the spcies data in regards to particle types, count, masses, and names. Additionally, it computes the mixture weights / number fractions.
The third block is further conversions and calculations required to make the later blocks execute smoothly. For example, defining the thermalSpeedScale as \(\sqrt{\frac{k_{b}T}{m}}\) makes later steps easier to read and follow. Ultimately, it determines the mixtureMeantransitFreq_Hz, which is used by the following block to determine audioHzPerTransitHz relative to the anchor frequency or based on the scale factor for the physical frequency.
Now, the function is able to compute probDensityPerHz! First, it determines the particle speed at each grid point. Then, it determines the Jacobian for the transformation from the speed domain to the frequency domain. After this, for each species, the Maxwell-Boltzmann distribution weight is applied for each grid point. Finally, this is convered into probDensityPerHz by summing across the numberFractions weighted distribution and their multiplication with the Jacobian.
Lastly, there are two diagnostic checks. First, it checks for speeds that would be present above the Nyquist frequency and an associated warning to the user if more than their inputted threshold of probability is lost above the Nyquist. Then, it checks whether the inputted parameters are actually classical, and warns the user if the Maxwell-Boltzmann distribution isn't accurate for their scenario.
Quantum Regime
Again, in order to input the quantum regime scenario into designLinearPhaseFir, we must obtain probDensityPerHz. The following functions are for this purpose.
buildQuantumDensity
Similarly to buildClassicalDensity, the first two code blocks obtain constants necessary for computation, as well as grabbing species, mass, spin, and statistical type from lookupQuantumSpecies.
Density of states prefactor determination for the following calculation:
The next code block simply grabs system parameters for input into later helper functions.
After this, the function calls solveContinuumChemicalPotential to obtain the chemical potential \(\mu\) for the system from the number density \(n = \frac{N}{V}\), which calls integrateOccupation to do. It then also calls integrateOccupation to obtain the mean energy of the system, which is necessary for the frequency mapping to follow.
For the frequency mapping, the function checks for absolute or relative mapping, and has a safety check if the mean energy is zero (which can occur during Bose-Einstein condensation). Then, it determines the conversion factor audioHzPerJoule.
Now, we can compute probDensityHz by the computaiton of the following:
The following block first computes the nyquistMomentum, which is simply the momentum associated with the Nyquist frequency. Then, the scale factor for conversion between the integral occupation form and probability is obtained.
To determine probDensityPerHz, which is our main goal, we must evaluate the occupation of each frequency cell in phase-space first. So, we convert our audio grid to an energy grid, and then convert it again to a momentum grid based on our particle mass. Then, we convert it again to velocity for \(\frac{dp}{de}\), which gives us all of the necessary values to compute probDensityPerHz! There's also a safeguard that I implemented because of some strange divergence behaviour at the zero frequency.
Lastly, before returning probDensityPerHz, we have a diagnostic check for lost probabilities above the Nyquist frequency, which compares the integral across all momenta vs the integral up to nyquistMomentum to let the user know if more than their inputted threshold of probability is lost.
solveContinuumChemicalPotential
solveContinuumChemicalPotential works by guessing the chemical potential and then refining the search. For the fermion case, it calls estimateFermiKineticEnergy, which determines the chemical potential by assuming the system is at absolute zero, which gives \(\mu\) as the integral is bounded by \(\mu = E_{F}\). The code then uses this as the upper bound for its search for the true \(\mu\). Then, it iterates over updating bounds to find when the integral evaluates to the number density.
The boson case works similarly, but first checks for the case in which the system is three-dimensional, in which it automatically fixes the max thermal density when \(\mu = 0\), as the one- and two-dimensional bosonic distributions will never condense.
It then checks if the number density is already greater than the max thermal density, which inidcates the divergence of probability in the ground state. If this is not the case, then the function will iteratively update its bounds to identify what \(\mu\) must be for the number density to be filled.
integrateOccupation
integrateOccupation is simply a helper function for the numerical integration of
continuumOccupation
continuumOccupation is simply a helper function for the distribution
Filter Design and Implementation
The following is mostly a re-hash and review of the aforementioned filter design process earlier in the document, however I thought it made sense to include here with the other function explanations, even if I'm repeating myself.
designLinearPhaseFir
This function operates by first treating the sampled magnitude as a zero-phase spectrum, and then mirrors it (MATLAB wants negative frequencies appended onto the end of the positive frequencies for ifft). Then, the real part of the Inverse Fourier Transform of the full, symmetric spectrum is obtained, which can be viewed as the real, symmetric impulse response, centred at n = 0!
After this, the impulse response is truncated according to the circular indices (the length of which is given by the user inputted numberOfTaps). Then, a window of the selected variety (Hanning, Hamming, or Blackman) is applied to the truncated kernel. Finally, the kernel is normalized according to the maximum amplitude of the input signal, and then the input signal is filtered according to these determined parameters!
shapeMagResp
This function is mostly a safety feature for any issues with Bose-Einstein condensation that might not have been caught earlier in the code (big spike to infinity at E = 0).
Basically, it determines the RMS of the distribution, and then clips the level according to the user inputted parameters, and then outputs the signal response. It also gives clippingInfo with some extra information regarding the clipping details, if the user desires.
Next Steps
The most obvious next step, is in my opinion, to create an app interface for this project. The current implementation method is very poor from a user-experience perspective, and thus I would argue the script as-is is quite inefficent for any applications other than curiosity. Ideally, this app interface would be continuously updating (such as through a slider input), but even an implementation that involves a more intuitive parameter menu and button to create the filter would be an improvement over the current version.
On the note of applicability, I think that another improvement that could be made would be the ability to do live input. However, there might be some issues regarding delay, as an 1025-tap FIR filter at 44.1kHz has a delay of 512 samples, and thus 11.61ms, which is audibly perceptible. In this scenario, it might be worth investigating the tradeoff between the exact frequency response offered by finite impulse response filters and the computational speed offered by infinite impulse response filters.