Wednesday, November 11, 2020

Designing De-emphasis Digital Filter for old CDs Part 3: Using Reference Equation

 There is a reference de-emphasis equation on this page (Thanks miguelito-san) : https://forums.stevehoffman.tv/threads/cd-dat-with-pre-emphasis-how-to-de-emphasize-correctly.88541/

TdB(f) =10log10( 1+ 1 (0.000015·2πf)2 1+ 1 (0.00005·2πf)2 )-10.4576……(1)
f : frequency (Hz)
TdB(f) : De-emphasis filter gain (dB)

This decibel is root power quantity therefore actual gain magnitude value T(f) is:

T(f) = 10TdB(f)/20……(2)
f : frequency (Hz)
T(f) : De-emphasis filter gain magnitude value at frequency f

Note 1: Math equations of this article uses MathML and it seems it is displayed correctly only on Firefox (as of November 2020).

Note 2: It seems 0.00005 and 0.000015 of equation 1 means 50 microseconds and 15 microseconds respectively and it is called 50/15 microsec emphasis.

On this article, equation (2) is used to create frequency sampling FIR digital filter.

Calculation steps are very similar to Part 2.

Example : Calculation of M=9 taps FIR filter coeffs using equation T(f)

Following is the procedure to design a linear-phase FIR filter using frequency sampling method. All the following calculation can be performed using  Excel spreadsheet.

When sampling frequency==44100 Hz and Desired FIR filter taps M==9,

Frequency sampling index k=0,1,2, ... , M-12 = 4, i.e.

k = 0, 1, 2, 3 and 4

We design de-emphasis filter for CD, therefore PCM sampling frequency = 44100Hz.

Frequency sampling angle frequency ωk=2πkM and its respective frequency is:

  • k = 0 :   ω0 = 0, freq0 = 0 Hz
  • k = 1 :   ω1 = 2π/9, freq1 = (44100/2π)(2π/9) = 44100 / 9 Hz = 4900 Hz
  • k = 2 :   ω2 = 4π/9, freq2 = (44100/2π)(4π/9) = 44100 * 2 / 9 Hz = 9800 Hz
  • k = 3 :   ω3 = 6π/9, freq3 = (44100/2π)(6π/9) = 44100 * 3 / 9 Hz = 14700 Hz
  • k = 4 :   ω4 = 8π/9, freq4 = (44100/2π)(8π/9) = 44100 * 4 / 9 Hz = 19600 Hz

Get filter gain on those frequencies Hr(ωk) using Equation (2):

  • Hr(ω0) = T(0) = 1
  • Hr(ω1) = T(4900) = 0.60004356
  • Hr(ω2) = T(9800) = 0.420524967
  • Hr(ω3) = T(14700) = 0.361602897
  • Hr(ω4) = T(19600) = 0.336724814

Then G(k) is calculated from Hr(ωk) by G(k) = (-1)k Hr(ωk) ……(3) :

  • G(0) = (-1)0 Hr(ω0) = 1
  • G(1) = (-1)1 Hr(ω1) = -0.60004356
  • G(2) = (-1)2 Hr(ω2) = 0.420524967
  • G(3) = (-1)3 Hr(ω3) = -0.361602897
  • G(4) = (-1)4 Hr(ω4) = 0.336724814

FIR filter coefficients h(k) can be calculated by h(k) = 1M{G(0)+2 n=1 (M-1)/2 G(n)cos2πn(k+1/2)M} …(4)

  • h(0) = 0.030212113446082472
  • h(1) = 0.040656939222222181
  • h(2) = 0.063594885887469199
  • h(3) = 0.11899203499978166
  • h(4) = 0.49308805288888891

Finally, this FIR filter is symmetry (linear phase), therefore h(8) = h(0), h(7) = h(1), h(6) = h(2), h(5) = h(3).

  • h(0) = 0.030212113446082472
  • h(1) = 0.040656939222222181
  • h(2) = 0.063594885887469199
  • h(3) = 0.11899203499978166
  • h(4) = 0.49308805288888891
  • h(5) = 0.11899203499978166
  • h(6) = 0.063594885887469199
  • h(7) = 0.040656939222222181
  • h(8) = 0.030212113446082472

We've got all the 9 FIR filter coefficients h(n) where n=0, 1, 2, ..., 8

Evaluating Frequency Response of the FIR filter obtained

Now one FIR filter is available for testing. FIR filter gain of arbitrary angular frequency ω can be calculated using the following equation:

Gain(ω) = k=0 M-1 h(k)e-jkω ……(5)

Equation (5) is complex number, which provides more info than we'd like to have (we need only frequency-gain response of the filter: the frequency-phase response is known to be linear with 4 samples of delay, this means frequency-phase error is zero for all frequencies when output signal is shifted by 4 samples to align to input signal). Real part and imaginary part of (5) can be calculated separately:

Gainreal(ω) = k=0 M-1 h(k)cos(-kω) ……(5r)
Gainimaginary(ω) = k=0 M-1 h(k)sin(-kω) ……(5i)

And the FIR filter Gain magnitude is calculated as follows: Gainmagnitude(ω) = Gainreal(ω)2 + Gainimaginary(ω)2 ……(6)

Finally Gain in decibel is: GaindB(ω) = 20log10{Gainmagnitude(ω)} ……(7)

For our M=9 FIR filter, frequency response can be calculated using equation (7). Using the reference equation (1), desirable gain of any frequency can be calculated, it is possible to compare gain values at as many frequency points as you wish. I compared 10Hz to 22040Hz semitone step frequency points and created Fig.1.


Fig.1. 9 taps FIR de-emphasis filter frequency response.

Comparing to the original de-emphasis table, max error of this FIR filter is 0.878 dB on 7360Hz, It is poor. The filtering quality can be improved by increasing M.

Searching the optimal FIR filter tap number M

FIR Filter Taps M Max Error (dB)
9 0.878
15 0.1704
17 0.115
19 0.0631
21 0.0522
23 0.0231
25 0.0272
27 0.00882

Table 1: FIR Filter taps and max error 



Fig.2 : Frequency Response of FIR Filter, taps=19  note: frequency axis is logarithmic.


Fig.3 : Frequency Response of FIR Filter, taps=27  note: frequency axis is logarithmic.


Fig.4 : Gain Error of FIR Filter, taps=27

 

From the Table 1, 

  • If the max error should be below 0.1 dB, use 19-tap FIR filter.
  • If the max error should be below 0.01 dB, use 27-tap FIR filter.

De-emphasis FIR Filter Coefficients for 44.1kHz PCM data

19-tap linear-phase FIR filter coefficients h(k) of max error < 0.1 dB is as follows:

0.0022333652179533105,
0.0027676001211334594,
0.0042218013139663415,
0.0065438712761329912,
0.011312237381544295,
0.01877355043394098,
0.03398288954901494,
0.059120380386441337,
0.11593361915957012,
0.49022137032060437,
0.11593361915957012,
0.059120380386441337,
0.03398288954901494,
0.01877355043394098,
0.011312237381544295,
0.0065438712761329912,
0.0042218013139663415,
0.0027676001211334594,
0.0022333652179533105,

27-tap linear-phase FIR filter coefficients h(k) of max error < 0.01 dB is as follows:

0.00031102739649091732,
0.00036885685453166665,
0.00057659816229764602,
0.00082952248115418167,
0.001449970925925961,
0.0022387507393955598,
0.0039420948394406248,
0.0063363475292175873,
0.011215231698621361,
0.018685854350970088,
0.033951734838472802,
0.059076192885731141,
0.11592376177923121,
0.49018811103703697,
0.11592376177923121,
0.059076192885731141,
0.033951734838472802,
0.018685854350970088,
0.011215231698621361,
0.0063363475292175873,
0.0039420948394406248,
0.0022387507393955598,
0.001449970925925961,
0.00082952248115418167,
0.00057659816229764602,
0.00036885685453166665,
0.00031102739649091732,

If you'd like to listen to poorly designed de-emphasis filter sound to compare to better one, use 9-tap filter coefficients.😀
 

Performing de-emphasis filtering to the pre-emphasised sound file using sox

With sox, it is possible to use those FIR filter coefficients rather direct fashion to filter the 44.1kHz PCM sound files. The following examples inputs inFile.flac and apply 27-tap de-emphasis FIR filter and output it as outFile.flac:

sox inFile.flac outFile.flac fir 0.00031102739649091732 0.00036885685453166665 0.00057659816229764602 0.00082952248115418167 0.001449970925925961 0.0022387507393955598 0.0039420948394406248 0.0063363475292175873 0.011215231698621361 0.018685854350970088 0.033951734838472802 0.059076192885731141 0.11592376177923121 0.49018811103703697 0.11592376177923121 0.059076192885731141 0.033951734838472802 0.018685854350970088 0.011215231698621361 0.0063363475292175873 0.0039420948394406248 0.0022387507393955598 0.001449970925925961 0.00082952248115418167 0.00057659816229764602 0.00036885685453166665 0.00031102739649091732

Search internet for "sox sound exchange" to get your sox copy. It is free of charge.

 

Next article: Creating Pre-emphasis Audio CD

3 comments:

  1. This is an excellent job! Is there a way of using your 27 taps FIR filter with software like sox of ffmpeg? Thansk!

    ReplyDelete
    Replies
    1. Hi I created FIR Filter app for FLAC files, FIRFilterConsole https://sourceforge.net/p/playpcmwin/wiki/FIRFilterConsole/

      Delete
    2. Added sox command example on the article.

      Delete