Faking a Static IP @ Home

Today I share my BASH script which automatically sends out an e-mail notification whenever the dynamic WAN IP address changes on your (Linux) system.

If you have a Linux box at home, and would like to know its address always, flawlessly, here is one possible solution not based on a third-party (paid) dynamic DNS service provider.

The script uses Ipify.org to fetch the WAN IP address, then sets up an authenticated SMTP session with an e-mail server of your choice and sends the IP address if it changes within intervals of 5 minutes. Note that the authentication require support for AUTH on the SMTP server. Also, you’ll need Perl on your Linux system for Base64 encoding of the authentication string.

To automatically start the script in the background on boot, add a cron entry as follows:

$ crontab -e

On a single line, enter:

@reboot /path/to/update_ip.sh
# update_ip.sh: Fake Static IP by geir.kjetil.nilsen@gmail.com (2020)

DOMAIN=my.e-mail.domain.goes.here #(example: hotmail.com)
SMTPHOST=my.e-mail.smtp-server.hostname.goes.here #(typically, the SMTP server hostname is smtp.$DOMAIN, e.g. smtp.hotmail.com)
SMTPPORT=465 #(SMTP port number)

MYHOSTNAME=my.local.hostname.goes.here #(free choice!)
MYAUTH=$(perl -MMIME::Base64 -e "print encode_base64(\"\000$MYUSER\@$DOMAIN\000$MYPASSWD\")")

while true; do
MYIP_NEW=$(curl 'https://api.ipify.org?format=text')
if [ "$MYIP" != "$MYIP_NEW" ];
if expr "$MYIP_NEW" : '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$' >/dev/null; then
# Send e-mail notification
#: '
(echo "ehlo $MYHOSTNAME";
sleep 1;
echo "auth plain $MYAUTH";
sleep 1;
echo "mail from:<$MYUSER@$DOMAIN>";
sleep 1;
echo "rcpt to:<$MYUSER@$DOMAIN>";
sleep 1;
echo "data";
sleep 1;
echo "subject: Oh, behave! $MYHOSTNAME's got a brand new IP!"
sleep 1;
echo $MYIP;
sleep 1;
echo ".";
sleep 1) | openssl s_client -crlf -connect $SMTPHOST:$SMTPPORT
echo "Discarding new IP - it is not valid."
# Wait 5 minutes...
sleep 300;



Coding a Phase Vocoder for Tempo Changing and Pitch Shifting in Audio Applications

One of my mathematics lecturers once invited us to solve a problem that has fascinated me ever since. If you create a digital sound clip by recording your own voice, how can you transform that to mimic the celebrated Norwegian weather forecaster Vidar Theisen?


For those of us never having heard about dear Mr. Theisen, or even wasn’t born before he passed away, you can watch one of his daily NRK weather forecasts below.

It is pretty obvious that Mr. Theisen had a very distinct way of talking. A voice of slow tempo accompanied by a monotonic pitch. Nobody, at least at that time, would be unsure who you pretended to be if you slowly and flat-voiced said something about the weather at Spitsbergen.

The lecturer also happened to be my adviser, and his name is Prof. Hans Munthe-Kaas. Hans is a very good man, and he has opened many doors for students that has knocked on his door. He is on my list of personal relationships I could not have been without.

To get the wheels rolling in his students heads, Mr. Munthe-Kaas outlined several incorrect strategies you could try to begin with: to slow things down by a factor two, try to repeat the digital samples one-by-one so that the net duration will be doubled. Or even better, try to interpolate?

It turns out that both strategies actually works in terms of doubling the duration, but has the inevitable side effect of also shifting down the pitch of audio signals. It is analogous to slowing down the rotational speed of a record player: it obviously decreases the tempo of the sound, but the tone will also be darker. If you happen to own a record player, you are probably nodding affirmative at this point. In other words, we approach Mr. Theisen in terms of tempo, but not by tone. For pure sinusoidal waves, what we want is to extend the number of periods rather than increasing the length of one period.

Red leaf

Virtually, the tempo and pitch of a tune is inseparable. If you change the tempo, the pitch will also change. The figure on the right illustrates the concept: modifications can seemingly only be done along the green diagonal line.

Until the mid 60s, this was accepted as the truth. Then suddenly a paper disturbed the community; two bright guys named Flanagan and Golden solved the problem in the context of telecommunication. Thanks to it, we can separate the two parameters. Controlling the Theisen tempo independently of the tone is now easy. And the invention’s name? The Phase Vocoder.

According to the theory, and some years of trial and error for my part, if what you want is to change the tempo of a signal by a factor Pt/Qt, and shift the pitch by a factor Pp/Qp, all you have to do is the following:

  1. Transform the signal over to the time-frequency plane using the Short-Time Fourier Transform (STFT).
  2. Linearly interpolate the magnitude of the STFT by a factor Pt/Qt*Qp/Pp across the time dimension.
  3. Go over the phase of the STFT and make sure that the partial time derivative across the time dimension is preserved.
  4. Transform the modified STFT back into the time domain
  5. Re-sample the resulting waveform by a factor Qp/Pp to get the final waveform.

We definitely need some test data to try this out. I have recorded my own voice saying the following in Norwegian:

“Som vi ser har det blitt kuldegrader over omtrent hele landet”.

Translated to English this corresponds to something like: “As you can see, we now have temperatures below zero degrees across the whole country”. You can listen to the original sound clip in the player below.

This Matlab code implements the Theisen Transform following the step-by-step Phase Vocoder recipe above:

function y = theisen(x)
% Geir K. Nilsen, 2006
% geir.kjetil.nilsen@gmail.com

Ns = length(x);
Nw = 1024;  % Window width
hop = Nw/4; % Overlap width
Pt = 2; 
Qt = 5;
Pp = 5; 
Qp = 6;
r = Pt/Qt * Qp / Pp; % Tempo-pitch shift factor

win = hann(Nw, 'periodic');

Nf = floor((Ns + hop - Nw) / hop);

fframe = zeros(Nw,Nf);
pframe = zeros(Nw,Nf);

% Step 1: STFT
c = 1;
for i = 0:hop:((Nf-1)*hop);
    fframe(:,c) = 2/3*fft(x(1+i:i+Nw).*win');
    c = c + 1;

% Step 2 & 3: Linear interpolation & phase preservation
phase = angle(fframe(:,1)); 
c = 1;
x = [fframe zeros(size(fframe, 1),1)]; 
for i = 0:r:Nf-1;                        
    x1 = x(:,floor(i)+1);                
    x2 = x(:,floor(i)+2);
    scale = i - floor(i);
    mag = (1-scale)*abs(x1) + scale*(abs(x2)); 
    pframe(:,c) = mag .* exp(j*phase); 
    c = c + 1;
    phase_adv = angle(x2) - angle(x1); 
    % Accumulate the phase
    phase = phase + phase_adv;

% Step 4: synthesize frames to get back waveform. Known as the Inverse
% Short Time Fourier Transform.
c = 1;
Nf = size(pframe,2);
Ns = Nf*hop - hop + Nw;
y = zeros(1,Ns);

for i = 0:hop:((Nf-1)*hop);
    pframe(:,c) = real(ifft(pframe(:,c)));          
    y(1+i:i+Nw) = y(1+i:i+Nw) + pframe(:,c)'.*win'; 
    c = c + 1;

% Step 5: finally resample the waveform to adjust according
% to the pitch shift factor.
y = resample(y, Pp, Qp);

When applied to the original recording, the result is quite impressing. Listen to the modified sound clip below and judge yourself.

For a practical C# implementation, see my Github repository.

The Resurrection of the Computer Fridge

img_20190731_1837407130935668874283186.jpgThe computer fridge hypothesis is a seemingly brilliant idea: store and operate your computer in a fridge, and wave good bye to all heating issues.

However — in 2015, a disappointing video appeared on YouTube: Linus Tech Tips once and for all rejected the computer fridge hypothesis by an experiment showing that refrigerators always loose in thermodynamic battles against modern computers.

The heat dissipated by the personal computer will exceed the heat absorption capacity of the fridge evaporator — causing the ambient temperature in the fridge to rise rather than fall. If we consider the system as a whole, we have a serious self-heating problem, and there is nothing we can do about it.

Or is it?

Cracking The Self-Heating Problem

Pleasant ideas deserve to be treated with respect, right? They should be tried from every angle before being trashed. From first principles, I decided to challenge the rejection of the computer fridge hypothesis by proposing: the ventilated computer fridge hypothesis!

The Ventilated Computer Fridge Hypothesis

A Ventilated Computer Fridge

The ventilated computer fridge hypothesis is based on the following reasoning: If the fridge is equipped with a balanced ventilation system, most of the heat dissipated by the computer will be transferred out of the fridge, so there will be no self-heating. When the fridge is turned off, the system will simply function as a normal computer cabinet. Now, if we in addition route the in-air stream via the evaporator, the air will be cold when it hits the computer, thus absorbing heat from the computer on the way out of the fridge. See the sketch on the right.

I decided to test the new hypothesis, so I had to build my own computer fridge.

Building a Computer Fridge

I bought a second hand mini-fridge with a glass door from TempTech on eBay as a starting point.

Installing Balanced Ventilation

bb138b1h3790128944658168010.jpgI decided to install a simple balanced ventilation system represented by four Corsair ML140 Pro cabinet fans.

While the task of installing four fans in a mini-fridge is not the most pleasant thing a human being can do in the world — it can be done by some laborious drilling on the outside, and heavy angle grinding on the inside.

My friend Øyvind Colliander created a nice template for the drilling part. See the fan hole template below. The trick is to tape a printed template to the fridge wall, and use a punch and a hammer to mark the center of the holes before drilling. For the angle grinding, put on a face mask and cut out four squares on the inside aligned with the holes on the outside.

Result: two fans on the upper back (in-air), and one fan on each side (out-air).

Fan holes on the sides

Fan holes on the back

Fan hole template for Corsair ML140 Pro (A4 print-friendly PDF)

Bypassing the Thermostat & More

In order to get full control of the fridge’s cooling system, I bypassed the internal thermostat and instead installed an Arduino micro controller and an external relay board beneath the back plate on the inside of the fridge. I also permanently installed the Arduino DHT-11 sensor to get ambient temperature and humidity readings. The sensor is placed in the bottom right corner of the fridge. I drilled holes on the backside for 230 VAC power and Ethernet, and installed a power socket, as well as two AC/DC converters (36 VDC & 12 VDC) on the inside.

Lighting and a Retractable Shelf

At this point, the fridge was ready to be tested, but for fun I decided to take the concept just a few steps further… Some lighting is nice, right? And — uh, a motorized retractable shelf for the computer! The latter turned out to be more challenging.

Basically, I used FreeCad to design a rack-and-pinion based retractable shelf. The design consists of three main parts; the rack, an old electric parabola antenna motor with a pinion, and a clamp to fix it to the shelf. Thanks to Andre Böehme for helping out with the toughest part — namely modelling the pinion and a matching rack tooth profile. I used i.materialise to 3D-print the parts. Great service!

Retractable Shelf Design

ejectTo control the position and direction of retraction of the shelf, I combined two of the relays on the external relay board to achieve a polarity reversal switch.

The parabola antenna motor also has a built-in pulse counter circuit, which can be used to pinpoint its exact angular position. Basically, the circuit sends out short square pulses synchronized with the pinions angular movement. Position control is thus achieved by simply counting pulses with an analog input channel on the Arduino.

See below for a full (sloppy) wiring diagram of the system.


Installing the Computer


I installed the computer hardware on a Lian Li test bench placed on top of the retractable shelf in the fridge.

Computer Specifications: 

  • ASUS ROG STRIX B450-E GAMING Motherboard
  • AMD Ryzen 5 2600 Wraith Stealth
  • Corsair Vengeance LPX DDR4-3200 C16 BK DC – 32GB
  • Samsung 970 EVO Plus SSD M.2 2280 – 500GB
  • ASUS GeForce RTX 2080 Ti ROG STRIX OC – 11GB GDDR6 RAM
  • Corsair 1000W PSU

Computer Fridge Code

The software I developed for the computer fridge basically consists of three parts;

  • An embedded Arduino back-end coded in C (deepfridge.ino)
  • A Python CLI frond-end to control the whole system (deepfridge.py)

Screenshot from 2019-08-09 11-47-03.png
Deep Fridge CLI

  • A set of Python test scripts with data logging functionality to conduct experiments (tests/deepfridge_logger_test*.py)

The code is available on Github: https://github.com/gknilsen/computer_fridge

The Computer Fridge Put at Test

Let’s run a series of tests to see if the computer fridge works. We will use the test scripts tests/deepfridge_logger_test*.py included in the Github repository. The test scripts log the following parameters to CSV files;

  • Fridge temperature  (by deepfridge.ino / Arduino DHT-11 sensor)
  • Fridge humidity (by deepfridge.ino / Arduino DHT-11 sensor)
  • CPU temperature (by calls to lmsensors / integrated CPU thermistor)
  • CPU utilization (by calls to cpu_usage.sh which is based on calls to top)
  • GPU temperature (by calls to nvidia-smi / integrated GPU thermistor)
  • GPU utilization (by calls to nvidia-smi)

Some of the tests put the CPU and/or GPU to maximum utilization under certain periods — this is achieved by calls to stress and gpu-burn.

Test #1 — Computer CPU & GPU at Idle with Cooling

Description: The computer CPU & GPU is left at idle. First run for 5 minutes when the compressor is OFF to get a baseline, then the compressor is turned ON, and the logging continues for 55 minutes. Total test time is 1 hour (3600 seconds).

Test script: tests/deepfridge_logger_test1.py

Log file: tests/test1.csv



Conclusion: Running the compressor for about an hour results in a CPU & GPU temperature drop of 5 degrees Celsius, when the computer is at idle. Also the ambient fridge temperature is reduced by 5 degrees Celsius.

Test #2 — Computer CPU at Maximum Utilization without Cooling

Description: The computer CPU is left at idle the first 5 minutes, and the compressor is OFF, to get a baseline. Then the computer CPU is put on maximum utilization for 5 minutes. The computer CPU is then left at idle for 5 minutes more minutes. Total test time is 15 minutes (900 seconds).

Test script: deepfridge_logger_test2.py

Log file: tests/test2.csv



Conclusion: Peak CPU temperature under max utilization is at 53.7 degrees Celsius.

Test #3 — Computer CPU at Maximum Utilization with Cooling

Description: The compressor is set ON, and the computer is left at idle the first 45 minutes. Then the computer CPU is put at maximum load for 5 minutes, followed by 10 more minutes at idle. Total test time is 60 minutes (3600 seconds).

Test script: deepfridge_logger_test3.py

Log file: tests/test3.csv



Conclusion: With constant cooling for about one hour & max CPU utilization for 5 minutes, again the CPU temperature is reduced by 5 degrees Celsius. Also the ambient fridge temperature is reduced by 5 degrees Celsius.

Test #4 — Computer GPU at Maximum Utilization without Cooling

Description: The computer GPU is left at idle the first 5 minutes, and the compressor is OFF, to get a baseline. Then the computer GPU is put at maximum utilization for 5 minutes. The computer GPU is then left at idle for 5 minutes more minutes. Total test time is 15 minutes (900 seconds).

Test script: deepfridge_logger_test4.py

Resulting log file: test4.csv



Conclusion: Peak GPU temperature under max utilization is at 66 degrees Celsius.

Test #5 — Computer GPU at Maximum Utilization with Cooling

Description: The compressor is set ON, and the computer is left at idle the first 45 minutes. Then the computer GPU is put on maximum load for 5 minutes, followed by 10 more minutes at idle. Total test time is 60 minutes (3600 seconds).

Test script: deepfridge_logger_test5.py

Log file: tests/test5.csv



Conclusion: With constant cooling for about one hour & max GPU utilization for 5 minutes, the GPU temperature is reduced by 3 degrees Celsius. The ambient fridge temperature is reduced by 4 degrees Celsius.


The tests show that there are no signs of self-heating, and that the CPU/GPU temperature drops 3-5 degrees Celsius when the cooling system has been activated for about an hour.

The ventilated computer fridge hypothesis has been confirmed!

However, is a temperature drop of 3-5 degrees Celsius worth the effort? Well, it depends. If you play around with overclocking and want to squeeze the most out of your system, 3-5 degrees Celsius might be enough. But is it really worth the effort? The system complexity is overwhelming, costly and not especially effective in its current state. However, the project was a great learning experience, and it was really fun! I doubt that I will run the cooling system much in its current state on a daily basis though.

There is plenty of room for optimization. In fact, currently I have not tested to see what happens if the cooling system remains activated for a longer period than one hour. Same is true for extended periods of maximum CPU/GPU utilization.

Please leave your thoughts in the comments section below. I am happy to receive ideas which can potentially improve the cooling performance of the system.

Melted Butter Cocoons – Part II

Short story: butter coccoon dry-aging attempt #1 as described in Melted Butter Coccoons – Part I was unsuccessful. The resulting steaks came out not rotten, but cigar-close to it.

Two key take-aways are

  1. Get hold of a fridge which is stable at 1 degree Celsius maximum.
  2. Get hold of meat which is guaranteed to not have undergone any form of prior wet aging.

To be honest, I have lost a bit of interest in this project, so I am not sure if there will be a second attempt. I have another project related to my PhD program in deep learning involving the dry-aging fridge (which obviously was not suitable/cold enough for this exercise). More on the new project soon!

Melted Butter Cocoons – Part I

Melted Butter Cocoons

I still remember the intense flavor and juiciness of that thick piece of Sirloin. Crusted in the name of Kona, Bone-In and Aged Dry. It was an exceptional dining experience at the Capitol Grill in Houston, Texas. It was the event that gave me the inspiration for this two-part blog post.

In this first part, I will introduce you to my little home project where I try to dry age Sirloin steaks at home. In part two, we will see the results. When? Well, in about 60 days my friend — when the meat is — say, middle — aged.

Dry Aging TL;DR

Dry aging of meat refers to the process of storing meat over a long period of time in a dry environment. This is different from traditional wet aging, where the meat is kept in a vacuum bag over a shorter period of time. Both ideas aim for increased tenderness, intensified flavor — not to mention the prolonged preservation time, which in fact is a common denominator behind many of the world’s culinary specialties. The Rakfisk of Norway is one golden example of this: trout is kept in a bin with sugar, salt and water. A controlled form of rotting process starts, and you can consume the fish over the course of months. One side effect is some very special taste and — uhm — smell properties! Same is true for many other specialties as well.

Dry aging is rooted in the same bio culinary paradigm as sour dough: you want your bio climate to be meat (or dough) friendly, and develop to the better over time. A good dry aging fridge (or room) can develop to the better over the years. It will be a unique bio climate colored by the natural microbiota in that environment.

Instead of going on to explain all the fancy sides of dry aging, I point you to Jess Pryles blog. Read on and you will learn everything you need to know. You will also notice a crucial “fact”: Dry aging is not for the home chef.


Because if you slice your meat into individual steaks before you proceed, they will basically come out as cured ham, eventually — dry and hard. Point is not to cure the meat! Original idea is to remove the outer dried layer before cooking and consumption. If you dry age individual steaks, they will be dry all the way through. Traditionally, dry aging calls for whole cuts of meat, and you will loose something like 5-10% from removing that outer dried layer after aging. So, either you go buy that large whole cut of meat, and accept the loss, or you stick to that boring wet aging, is it right?

Not quite! It turns out that Casper Stuhr Cobzcyk has developed a new dry aging technique where he first dips his (still) whole cuts of meat in melted butter to form a cocoon protecting the steak from drying out on the surface. It reminds me about cheese waxing!

If you combine the thoughts of Pryles and Stuhr Cobzcyk, I figured it would allow for dry aging of individual steaks at home, since the melted butter cocoon will protect the individual steaks from drying out at the surface, and, hence, you will not need to remove the outer layer nor will they be cured!

Butter probably acts like sort of a “let-moist-out, not-let-oxygen in”-barrier. This in turn, brings up a lot of questions Mr. Stuhr Cobzcyk does not answer. He probably has them, but why should he really share them with the world. For example, what is the optimal thickness of the butter cocoon? Unfortunately, I don’t know all the answers yet.

I decided to put strings around my two steaks, before dipping them in. This allowed me to hang them in the fridge rather than letting them sink into the rack and potentially create a butter mess.

Dry aging started: 27th of October, 2018.

We aim for 60 days, so dry aging stops: 26th of December, 2018.

A Double Thank You

I was reading at the library when I got a phone call from my boss. She asked me to immediately return to home because of a not-so-unusual & escalating disobedient kids situation.

I got onto my feet and started to pack my bag. I eyed the cord of my laptop charger, the way it snaked itself over the floor, and ended up in a socket overly close to the feet of a random lady.

I approached her and gesticulated it would require her attention to resolve the situation. She read it and started to unplug my charger but was not able to get it out. She gave up quickly, and I had to help her. And so I successfully managed to unplug it under grant of an informal intimate zone intrusion permission. Once unplugged, I said, “Thank You”, and she responded “Thank You”.

It was an interesting Double Thank You for the help situation: I thanked her for permitting me to enter her intimate zone, and I thanked her for her initial failed attempt of helping me. Then she thanked me back for being polite and helpful enough to help her when helping me.

Fat Man & Little Boy

On a sunny spring day, I craved for steak and went to see my good old barbecue on the patio. But I found it ill-conditioned. The rain in Bergen had finally taken its toll on the rusty little boy: the price of storing and using it outside during winters now had to be paid. So I questioned myself whether to buy a new one a larger and better one. Filled with ambivalence, I went to the retailer and found a big fat man. Upgrading is a bliss. Ka-ching.

Some years ago I used to work for an oil & gas company. In that context — I visited a site we had some cross-business activity with — located in Houston, Texas. Upon arrival I was told to settle in the empty cubicle of another engineer on vacation. On the cube wall I noticed a drawing showing a comparison between two versions of the site’s main product: the legacy 15k psi ram BOP (Blow-Out Preventer), and the next 20k psi generation. Let me add that the size of such equipment does not grow linearly with the pressure rating, so the next version was a fat man compared to the little boy.

Typically, when bad oil spill disasters take place, the BOP is likely to be in the spot. The BOP acts as the main security system resting on top of the very well head. They are used to seal wells under normal conditions, as well as in emergency situations. In case of the latter, the BOPs have strong hydraulic shear rams fitted with sharp steel blades. They can cut through any solid obstacles (e.g. drill pipe) situated — intentionally or not — in the bore. The aim is to seal the well and prevent oil spill, or even blow-outs on the rig deck — usually leading to fire, havoc and death.

I noticed, printed with small letters beneath each of the two drawings; “Little Boy”, “Fat Man”. It took me a couple of seconds before I realized the meaning. Call it irony, sarcasm or blasphemy — the reference was to the respective nukes detonated by US over Hiroshima and Nagasaki in 1945.

It also got me thinking about the two I now have on the patio back home.

Fat Man & Little Boy

Device Equals ANSI Dot Sys

Click here to download the above image in android wallpaper format!

As a kid I figured out how to edit my father’s config.sys and autoexec.bat to change font colors in MS-DOS. Without the help from the internet, it was such a mysterious age where small technological discoveries were made based solely on trial and error, gossip, obscure scripts, tools and white papers spread on LAN parties and floppies.

The trick was to load the binary file ansi.sys using the DEVICE= directive in config.sys, enabling escape sequence interpretation to the standard output — which included means for changing the background and foreground font color on the command prompt.

I later installed Red Hat Linux 5.2 in 1998, but after a while I ended up compiling my own “distro” based on Linux from Scratch. My favourite nostalgic piece of memory is still my only ANSI art creation to date, namely my /etc/issue (image shown above), born based on piping and overlying Tux from Welcome2L (c) LittleIgloo.Org on a classical Windows 9x blue screen of death replica!

UPDATED 17/8-18: Added Android wallpaper version of BSOD Tux.

The Ashlad’s Older Brothers

I tend to notice small things in life. Combined with some pondering, they can make me unusually happy. Today I proudly present one of my favourite curiosities of all time:

The priceless, harassing laughter of the Ashlad’s older brothers.

The sound clip is taken from the legendary stop-motion animated short by Ivo Caprino, which is based on the Norwegian folk tale “Askeladden og De Gode Hjelperne” (English: “The Ashlad and His Good Helpers”) [ref. 02:43].

For some reason, DailyMotion.Com has taken the liberty to publish the full original movie online. I believe Caprino would have flipped in his grave if he knew. And I wouldn’t be surprised if his family company considers even more advanced gymnastics alive!

Coding a Parametric Equalizer for Audio Applications

As a kid I was very fascinated by a particular type of electronic devices known as equalizers. These devices are used to adjust the balance between frequency components of electronic signals, and has a widespread application in audio engineering.

As I grew up the fascination continued, but shifted from wanting to own one — into wanting to build one — into wanting to code one.

Image credit: http://www.lifewire.com


In this article I will explain how you can code your own audio equalizer, enabling you to integrate your own variant in your own projects. Let us start with some background information and basic theory.

When you use the volume knob to pump up the volume on your stereo, it will boost all the frequency components of the audio by roughly the same amount. The bass and treble controls on some stereos take this concept one step further; they divide the whole frequency range into two parts — where the bass knob controls the volume in the lower frequency range and the treble knob controls the volume in the upper frequency range.

Now, with an audio equalizer, you have the possibility to adjust the volume on any given number of  individual frequency ranges, separately.

Physically, the front panel of an equalizer device typically consists of a collection of slider knobs, each corresponding to a given frequency range — or more specifically — to a given center frequency. The term center frequency refers to the mid point of the frequency range the slider knob controls.

By arranging the sliders in increasing center frequency order, the combined positions of the individual sliders will represent the overall frequency response of the equalizer. This is where it gets interesting, because the horizontal position of the slider now represents frequency, and the vertical position represents the response modification you wish to impose on that frequency. In other words, you can “draw” your desired frequency response by arranging the sliders accordingly.

Theory of Parametric Equalizers

An additional degree of freedom arises when the center frequency per slider also is adjustable. This is exactly what a parametric equalizer is: it lets the user specify a number of sections (think of section here as a slider), each with a frequency response adjustable by the following parameters: center frequency (f0), bandwidth (Bf), bandwidth gain (GB), reference gain (G0) and boost/cut gain (G):

  • The center frequency (f0) represents the mid-point of the section’s frequency range and is given in Hertz [Hz].
  • The bandwidth (Bf) represents the width of the section across frequency and is measured in Hertz [Hz]. A low bandwidth corresponds to a narrow frequency range meaning that the section will concentrate its operation to only the frequencies close to the center frequency. On the other hand, a high bandwidth yields a section of wide frequency range — affecting a broader range of frequencies surrounding the center frequency.
  • The bandwidth gain (GB) is given in decibels [dB] and represents the level at which the bandwidth is measured. That is, to have a meaningful measure of bandwidth, we must define the level at which it is measured. See Figure 1.
  • The reference gain (G0) is given in decibels [dB] and simply represents the level of the section’s offset. See Figure 1.
  • The boost/cut gain (G) is given in decibels [dB] and prescribes the effect imposed on the audio loudness for the section’s frequency range. A boost/cut level of 0 dB corresponds to unity (no operation), whereas negative numbers corresponds to cut (volume down) and positive numbers to boost (volume up).

Figure 1: Section Frequency Spectrum

A section is really just a filter — in our case a digital audio filter with the parameters corresponding to the elements in the list above.

Implementation in Matlab

The abstraction now is the following: a parametric audio equalizer is nothing else than a list of digital audio filters acting on the input signal to produce an output signal with the desired balance between frequency components.

This means that the smallest building block we need to create is a digital audio filter. Without going deep into the field of digital filter design, I will make it easy for you and jump straight to the crucial equation required:

Equation 1

a0*y(n) = b0*x(n) + b1*x(n-1) + b2*x(n-2)
        - a1*y(n-1) - a2*y(n-2), for n = 0, 1, 2, ...

In equation 1, x(n) represents the input signal, y(n) the output signal, a0, a1 and a2, are the feedback filter coefficients, and b0, b1 and b2, are the feedforward filter coefficients.

To calculate the filtered output signal, y(n), all you have to do is to run the input signal, x(n), through the recurrence relation given by equation 1. In Matlab, equation 1 corresponds to the filter function. We will come back to it shortly, but first we need to get hold of the filter coefficients (a0, a1, a2, b0, b1 and b2).

At this point, you can read Orfanidis’ paper and try to grasp the underlying mathematics, but I will make it easy for you. Firstly, we define the sampling rate of the input signal x(n) as fs, and secondly, by using the section parameters defined above, the corresponding filter coefficients can be calculated by

Equations 2 – 8

beta = tan(Bf/2*pi/(fs/2))*sqrt(abs((10^(GB/20))^2
     - (10^(G0/20))^2))/sqrt(abs(10^(G/20)^2 - (10^(GB/20))^2))

b0 = (10^(G0/20) + 10^(G/20)*beta)/(1+beta)
b1 = -2*10^(G0/20)*cos(f0*pi/(fs/2))/(1+beta) 
b2 = (10^(G0/20) - 10^(G/20)*beta)/(1+beta)

a0 = 1
a1 = -2*cos(f0*pi/(fs/2))/(1+beta)
a2 = (1-beta)/(1+beta)

Note that beta in equation 2 is just used as an intermediate variable to simplify the appearance of equations 3 through 8. Also note that tan() and cos() represents the tangens and cosine functions, respectively, pi represents 3.1415…, and sqrt() is the square root.

As an example, if we define the following section parameters:

(fs, f0, Bf, GB, G0, G) = (1000, 250, 40, 9, 0, 12)

It means that we will have a section operating at a 1 kHz sampling rate with a center frequency of 250 Hz, bandwidth of 40 Hz, bandwidth gain of 9 dB, reference gain of 0 dB and boost gain of 12 dB. See Figure 2 for the frequency response (spectrum) of the section.

Figure 2: Example Section Frequency Spectrum

Let’s say we have defined a list of many sections. How do we combine all the sections together so we can see the overall result? The following Matlab script illustrates the concept by setting up a 4-section parametric equalizer.

% Parametric Equalizer by Geir K. Nilsen (2017)
clear all;

fs = 1000; % Sampling frequency [Hz]
S = 4; % Number of sections

Bf = [5 5 5 5]; % Bandwidth [Hz]
GB = [9 9 9 9]; % Bandwidth gain (level at which the bandwidth is measured) [dB]
G0 = [0 0 0 0]; % Reference gain @ DC [dB]
G = [8 10 12 14]; % Boost/cut gain [dB]
f0 = [200 250 300 350]; % Center freqency [Hz]

h = [1; zeros(1023,1)]; % ..for impulse response
b = zeros(S,3); % ..for feedforward filter coefficients
a = zeros(S,3); % ..for feedbackward filter coefficients

for s = 1:S;
    % Equation 2
    beta = tan(Bf(s)/2 * pi / (fs / 2)) * sqrt(abs((10^(GB(s)/20))^2 - (10^(G0(s)/20))^2)) / sqrt(abs(10^(G(s)/20)^2 - (10^(GB(s)/20))^2));
    % Equation 3 through 5
    b(s,:) = [(10^(G0(s)/20) + 10^(G(s)/20)*beta), -2*10^(G0(s)/20)*cos(f0(s)*pi/(fs/2)), (10^(G0(s)/20) - 10^(G(s)/20)*beta)] / (1+beta);
    % Equation 6 through 8
    a(s,:) = [1, -2*cos(f0(s)*pi/(fs/2))/(1+beta), (1-beta)/(1+beta)];

    % apply equation 1 recursively per section.
    h = filter(b(s,:), a(s,:), h);

% Plot the frequency spectrum of the combined section impulse response h
H = db(abs(fft(h)));
H = H(1:length(H)/2);
f = (0:length(H)-1)/length(H)*fs/2;
axis([0 fs/2 0 20])
xlabel('Frequency [Hz]');
ylabel('Gain [dB]');
grid on

The key to combining the sections is to run the input signal through the sections in a cascaded fashion: the output signal of the first section is fed as input to the next section, and so on. In the script above, the input signal is set to the delta function, so that the output of the first section yields its impulse response — which in turn is fed as the input to the next section, and so on. The final output of the last section is therefore the combined (total) impulse response of all the sections, i.e. the impulse response of the parametric equalizer.

The FFT is then applied on the overall impulse response to calculate the frequency response, which finally is used to produce the frequency spectrum of the equalizer shown in Figure 3.

eq response
Figure 3: Parametric Equalizer Frequency Spectrum

The next section of this article will address how to make the step from the Matlab implementation above to a practical implementation in C#. Specifically, I will address:

  • How to run the equalizer in real-time, i.e. how to apply it in a system where only few samples of the actual input signal is available at the same time — and furthermore ensure that the combined output signals will be continuous (no hiccups).
  • How to object orientate the needed parts of the implementation.

Real-time considerations

Consider equation 1 by evaluating it for n=0,1,2, and you will notice that some of the  indices on the right hand side of the equation will be negative. These terms with negative index correspond to the recurrence relation’s initial conditions. If we consider the recurrence relation as a one-go operation, it is safe to set those terms to zero. But what if we have a real system sampling a microphone at a rate of fs=1000 Hz, and where the input signal x(n) is made available in a finite length buffer of size 1000 samples — updated by the system once every second?

To ensure that the combined output signals will be continuous, the initial conditions must be set based on the previous states of the recurrence relation. In other words, the recurrence relation implementation must have some memory of its previous states. Specifically, it means that at the end of the function implementing the recurrence relation, one must store the two last samples of the current output and input signals. When the next iteration starts, it will use those values as the initial conditions y(-1), y(-2), x(-1) and x(-2).

A practical C# Implementation

I will now give a practical implementation in C#. It consists of three classes

  • Filter.cs — Implements the recurrence relation given in equation 1. And yes, it is made to deal gracefully with the initial condition problem stated in the previous section.
  • Section.cs — Implements a section as described by the parameters listed previously.
  • ParametricEqualizer.cs — Implements the parametric equalizer.


/* @author Geir K. Nilsen (geir.kjetil.nilsen@gmail.com) 2017 */

namespace ParametricEqualizer
    public class Filter
        private List a;
        private List b;

        private List x_past;
        private List y_past;

        public Filter(List a, List b)
            this.a = a;
            this.b = b;

        public void apply(List x, out List y)
            int ord = a.Count - 1;
            int np = x.Count - 1;

            if (np < ord)
                for (int k = 0; k < ord - np; k++)
                np = ord;

            y = new List();
            for (int k = 0; k < np + 1; k++)

            if (x_past == null)
                x_past = new List();

                for (int s = 0; s < x.Count; s++)

            if (y_past == null)
                y_past = new List();

                for (int s = 0; s < y.Count; s++)

            for (int i = 0; i < np + 1; i++)
                y[i] = 0.0;

                for (int j = 0; j < ord + 1; j++)
                    if (i - j < 0)
                        y[i] = y[i] + b[j] * x_past[x_past.Count - j];
                        y[i] = y[i] + b[j] * x[i - j];

                for (int j = 0; j < ord; j++)
                    if (i - j - 1 < 0)
                        y[i] = y[i] - a[j + 1] * y_past[y_past.Count - j - 1];
                        y[i] = y[i] - a[j + 1] * y[i - j - 1];

                y[i] = y[i] / a[0];

            x_past = x;
            y_past = y;



/* @author Geir K. Nilsen (geir.kjetil.nilsen@gmail.com) 2017 */

namespace ParametricEqualizer
    public class Section
        private Filter filter;
        private double G0;
        private double G;
        private double GB;
        private double f0;
        private double Bf;
        private double fs;

        private double[][] coeffs;

        public Section(double f0, double Bf, double GB, double G0, double G, double fs)
            this.f0 = f0;
            this.Bf = Bf;
            this.GB = GB;
            this.G0 = G0;
            this.G = G;
            this.fs = fs;

            this.coeffs = new double[2][];
            this.coeffs[0] = new double[3];
            this.coeffs[1] = new double[3];

            double beta = Math.Tan(Bf / 2.0 * Math.PI / (fs / 2.0)) * Math.Sqrt(Math.Abs(Math.Pow(Math.Pow(10, GB / 20.0), 2.0) - Math.Pow(Math.Pow(10.0, G0 / 20.0), 2.0))) / Math.Sqrt(Math.Abs(Math.Pow(Math.Pow(10.0, G / 20.0), 2.0) - Math.Pow(Math.Pow(10.0, GB/20.0), 2.0)));

            coeffs[0][0] = (Math.Pow(10.0, G0 / 20.0) + Math.Pow(10.0, G/20.0) * beta) / (1 + beta);
            coeffs[0][1] = (-2 * Math.Pow(10.0, G0/20.0) * Math.Cos(f0 * Math.PI / (fs / 2.0))) / (1 + beta);
            coeffs[0][2] = (Math.Pow(10.0, G0/20) - Math.Pow(10.0, G/20.0) * beta) / (1 + beta);

            coeffs[1][0] = 1.0;
            coeffs[1][1] = -2 * Math.Cos(f0 * Math.PI / (fs / 2.0)) / (1 + beta);
            coeffs[1][2] = (1 - beta) / (1 + beta);

            filter = new Filter(coeffs[1].ToList(), coeffs[0].ToList());

        public List run(List x, out List y)
            filter.apply(x, out y);
            return y;


namespace ParametricEqualizer
    public class ParametricEqualizer
        private int numberOfSections;
        private List section; 
        private double[] G0; 
        private double[] G; 
        private double[] GB; 
        private double[] f0; 
        private double[] Bf; 

        public ParametricEqualizer(int numberOfSections, int fs, double[] f0, double[] Bf, double[] GB, double[] G0, double[] G) 
            this.numberOfSections = numberOfSections; 
            this.G0 = G0; 
            this.G = G; 
            this.GB = GB; 
            this.f0 = f0; 
            this.Bf = Bf; 
            section = new List();
            for (int sectionNumber = 0; sectionNumber < numberOfSections; sectionNumber++) 
                section.Add(new Section(f0[sectionNumber], Bf[sectionNumber], GB[sectionNumber], G0[sectionNumber], G[sectionNumber], fs));
        public void run(List x, ref List y) 
            for (int sectionNumber = 0; sectionNumber < numberOfSections; sectionNumber++) 
                section[sectionNumber].run(x, out y); 
                x = y; // next section 

Usage Example

Let’s conclude the article by an example where we create a ParamtricEqualizer object and applies it on some input data. The following snippet will setup the equivalent 4-section equalizer as in the Matlab implementation above.

double[] x = new double[] { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; // input signal (delta function example)
List y = new List(); // output signal
ParametricEqualizer.ParametricEqualizer peq = new ParametricEqualizer.ParametricEqualizer(4, 1000, new double[] { 200, 250, 300, 350 }, new double[] { 5, 5, 5, 5 }, new double[] { 9, 9, 9, 9 }, new double[] {0, 0, 0, 0}, new double[] {8, 10, 12, 14});
peq.run(x.ToList(), ref y);