Home
About Us
%-ile Freq Macros
Notes on SQ Topics
Projects & Examples
Contact Us
   
 

EXCEL macro code for Percentile Frequency Calculations                                 (Scroll Down for Cband8 and Cband10 macros)

The following is macro code for calculating Percentile Frequencies from Specific Loudness Patterns imported into a Microsoft EXCEL spreadsheet. 

Sub Perc_Freq_Row()
   'Calculates Percentile Frequencies for Zwicker Specific Loudness Patterns
   'INPUT: Expects one Pattern per Row  (The User must select the Range containing the Patterns)
   'Calculates total area under Pattern and Seven %-ile Frequencies (as line numbers)
   '(Line numbers refer to the presentation of the Critical Bands)
   'For example:
   '   Head Acoustics uses 10 lines per band for a total of 240 lines per pattern
   '   B&K uses 8 lines per band for a total of 192 lines per pattern
   'OUTPUT: After skipping one column to the right of the Pattern data, 
   'the macro displays the results in seven (7) columns

   Dim Msg
   Dim Msg2

    'Identify the Calculation Range
    startcol = Selection.Column
    numcols = Selection.Columns.Count
    startrow = Selection.Row
    numrows = Selection.Rows.Count
  If numcols = 192 Or numcols = 240 Then
    For rowind = startrow To startrow + numrows – 1

      'Initialize Variables for each pattern (row)
      Npatsum = 0
      Pct10 = 0
      Pct20 = 0
      Pct30 = 0
      Pct50 = 0
      Pct70 = 0
      Pct80 = 0
      Pct90 = 0
      Flag1 = 0
      Flag2 = 0
      Flag3 = 0
      Flag5 = 0
      Flag7 = 0
      Flag8 = 0
      Flag9 = 0
      freqsum = 0

      'Determine the Total Area under the Pattern (Npatsum)
    For colind = startcol To startcol + numcols - 1
      Npatsum = Npatsum + ActiveSheet.Cells(rowind, colind).Value
    Next colind

    'Determine the Percentile Frequencies (line numbers) for the Pattern (Row)
    'Incrementally add the cells (freqsum) and compare to the Total Pattern Area
    'Flags are set after each Percentile Frequency is found
    'Output the results in columns to the right of the Calculation Range
    I = 0
    For colind = startcol To startcol + numcols - 1
      I = I + 1
      freqsum = freqsum + ActiveSheet.Cells(rowind, colind).Value
      If Flag1 = 0 Then
        If freqsum >= Npatsum * 0.1 Then
          Pct10 = I
          Flag1 = 1
          ActiveSheet.Cells(rowind, startcol + numcols + 1).Value = Pct10
        End If
      End If
      If Flag2 = 0 Then
        If freqsum >= Npatsum * 0.2 Then
          Pct20 = I
          Flag2 = 1
          ActiveSheet.Cells(rowind, startcol + numcols + 2).Value = Pct20
        End If
      End If
      If Flag3 = 0 Then
        If freqsum >= Npatsum * 0.3 Then
          Pct30 = I
          Flag3 = 1
          ActiveSheet.Cells(rowind, startcol + numcols + 3).Value = Pct30
        End If
      End If
      If Flag5 = 0 Then
        If freqsum >= Npatsum * 0.5 Then
          Pct50 = I
          Flag5 = 1
          ActiveSheet.Cells(rowind, startcol + numcols + 4).Value = Pct50
        End If
      End If
      If Flag7 = 0 Then
        If freqsum >= Npatsum * 0.7 Then
          Pct70 = I
          Flag7 = 1
          ActiveSheet.Cells(rowind, startcol + numcols + 5).Value = Pct70
        End If
      End If
      If Flag8 = 0 Then
        If freqsum >= Npatsum * 0.8 Then
          Pct80 = I
          Flag8 = 1
          ActiveSheet.Cells(rowind, startcol + numcols + 6).Value = Pct80
        End If
      End If
      If Flag9 = 0 Then
        If freqsum >= Npatsum * 0.9 Then
          Pct90 = I
          Flag9 = 1
          ActiveSheet.Cells(rowind, startcol + numcols + 7).Value = Pct90
        End If
      End If     
    Next colind
    Next rowind
 
    Msg2 = "Add %-ile Freq Labels above the data columns?"
    Response = MsgBox(Msg2, vbYesNo, "%-ile Frequency Labels")
    If Response = vbYes Then
      ActiveSheet.Cells(startrow - 1, startcol + numcols + 1).Value = "10%Freq"
      ActiveSheet.Cells(startrow - 1, startcol + numcols + 2).Value = "20%Freq"
      ActiveSheet.Cells(startrow - 1, startcol + numcols + 3).Value = "30%Freq"
      ActiveSheet.Cells(startrow - 1, startcol + numcols + 4).Value = "50%Freq"
      ActiveSheet.Cells(startrow - 1, startcol + numcols + 5).Value = "70%Freq"
      ActiveSheet.Cells(startrow - 1, startcol + numcols + 6).Value = "80%Freq"
      ActiveSheet.Cells(startrow - 1, startcol + numcols + 7).Value = "90%Freq"
    End If
  Else

  Msg = "The Selected Range must contain EXACTLY either 192 or 240 columns."
  Msg = Msg & Chr$(10) & "PLEASE RE-SELECT THE RANGE & TRY AGAIN"
  MsgBox Msg, vbExclamation, "Range Selection Error"

End If
End Sub

Cband8 – an EXCEL macro to convert B&K line numbers to kHz

Sub Cband8()
   '
   'Written by: Richard J. Fridrich 05AU99
   '
   ' For %-ile Freq's from B&K Data which uses 8 lines per band for a total of 192 lines per pattern
   ' Replaces line numbers (integers from 1 to 192) with frequency values (kHz)
   '
Dim Msg
Dim Cb192(192)

Cb192(1) = 0.02
Cb192(2) = 0.03
Cb192(3) = 0.04
Cb192(4) = 0.05
Cb192(5) = 0.059
Cb192(6) = 0.069
Cb192(7) = 0.079
Cb192(8) = 0.088    ' 1

Cb192(9) = 0.1
Cb192(10) = 0.113
Cb192(11) = 0.125
Cb192(12) = 0.137
Cb192(13) = 0.15
Cb192(14) = 0.162
Cb192(15) = 0.175
Cb192(16) = 0.187  ' 2

Cb192(17) = 0.2
Cb192(18) = 0.212
Cb192(19) = 0.225
Cb192(20) = 0.237
Cb192(21) = 0.25
Cb192(22) = 0.262
Cb192(23) = 0.275
Cb192(24) = 0.288  ' 3

Cb192(25) = 0.301
Cb192(26) = 0.314
Cb192(27) = 0.328
Cb192(28) = 0.341
Cb192(29) = 0.354
Cb192(30) = 0.367
Cb192(31) = 0.38
Cb192(32) = 0.394  ' 4

Cb192(33) = 0.407
Cb192(34) = 0.42
Cb192(35) = 0.433
Cb192(36) = 0.446
Cb192(37) = 0.46
Cb192(38) = 0.473
Cb192(39) = 0.487
Cb192(40) = 0.501  ' 5

Cb192(41) = 0.515
Cb192(42) = 0.528
Cb192(43) = 0.542
Cb192(44) = 0.556
Cb192(45) = 0.571
Cb192(46) = 0.586
Cb192(47) = 0.602
Cb192(48) = 0.617  ' 6

Cb192(49) = 0.633
Cb192(50) = 0.649
Cb192(51) = 0.664
Cb192(52) = 0.68
Cb192(53) = 0.695
Cb192(54) = 0.711
Cb192(55) = 0.729
Cb192(56) = 0.747  ' 7

Cb192(57) = 0.766
Cb192(58) = 0.784
Cb192(59) = 0.802
Cb192(60) = 0.82
Cb192(61) = 0.838
Cb192(62) = 0.857
Cb192(63) = 0.875
Cb192(64) = 0.893  ' 8

Cb192(65) = 0.913
Cb192(66) = 0.934
Cb192(67) = 0.955
Cb192(68) = 0.976
Cb192(69) = 0.997
Cb192(70) = 1.02
Cb192(71) = 1.04
Cb192(72) = 1.06      ' 9

Cb192(73) = 1.08
Cb192(74) = 1.1
Cb192(75) = 1.12
Cb192(76) = 1.15
Cb192(77) = 1.17
Cb192(78) = 1.2
Cb192(79) = 1.22
Cb192(80) = 1.25 ' 10

 

Cb192(81) = 1.27
Cb192(82) = 1.3
Cb192(83) = 1.32
Cb192(84) = 1.35
Cb192(85) = 1.37
Cb192(86) = 1.4
Cb192(87) = 1.43
Cb192(88) = 1.46 ' 11

Cb192(89) = 1.49
Cb192(90) = 1.52
Cb192(91) = 1.54
Cb192(92) = 1.57
Cb192(93) = 1.6
Cb192(94) = 1.63
Cb192(95) = 1.66
Cb192(96) = 1.69 ' 12

Cb192(97) = 1.72
Cb192(98) = 1.75
Cb192(99) = 1.78
Cb192(100) = 1.81
Cb192(101) = 1.85
Cb192(102) = 1.88
Cb192(103) = 1.92
Cb192(104) = 1.96 ' 13

Cb192(105) = 1.99
Cb192(106) = 2.03
Cb192(107) = 2.07
Cb192(108) = 2.1
Cb192(109) = 2.14
Cb192(110) = 2.18
Cb192(111) = 2.21
Cb192(112) = 2.25 ' 14

Cb192(113) = 2.3
Cb192(114) = 2.35
Cb192(115) = 2.4
Cb192(116) = 2.45
Cb192(117) = 2.5
Cb192(118) = 2.55
Cb192(119) = 2.6
Cb192(120) = 2.65 ' 15

Cb192(121) = 2.7
Cb192(122) = 2.75
Cb192(123) = 2.8
Cb192(124) = 2.86
Cb192(125) = 2.93
Cb192(126) = 2.99
Cb192(127) = 3.05
Cb192(128) = 3.11 ' 16
 
Cb192(129) = 3.18
Cb192(130) = 3.24
Cb192(131) = 3.3
Cb192(132) = 3.36
Cb192(133) = 3.43
Cb192(134) = 3.49
Cb192(135) = 3.55
Cb192(136) = 3.64 ' 17

Cb192(137) = 3.72
Cb192(138) = 3.81
Cb192(139) = 3.89
Cb192(140) = 3.98
Cb192(141) = 4.06
Cb192(142) = 4.14
Cb192(143) = 4.23
Cb192(144) = 4.31 ' 18

Cb192(145) = 4.4
Cb192(146) = 4.48
Cb192(147) = 4.59
Cb192(148) = 4.7
Cb192(149) = 4.82
Cb192(150) = 4.93
Cb192(151) = 5.05
Cb192(152) = 5.16 ' 19

Cb192(153) = 5.28
Cb192(154) = 5.39
Cb192(155) = 5.5
Cb192(156) = 5.62
Cb192(157) = 5.77
Cb192(158) = 5.91
Cb192(159) = 6.05
Cb192(160) = 6.2  ' 20

Cb192(161) = 6.34
Cb192(162) = 6.49
Cb192(163) = 6.63
Cb192(164) = 6.77
Cb192(165) = 6.92
Cb192(166) = 7.06
Cb192(167) = 7.24
Cb192(168) = 7.44 ' 21

Cb192(169) = 7.64
Cb192(170) = 7.83
Cb192(171) = 8.03
Cb192(172) = 8.23
Cb192(173) = 8.43
Cb192(174) = 8.62
Cb192(175) = 8.82
Cb192(176) = 9.03 ' 22

Cb192(177) = 9.33
Cb192(178) = 9.64
Cb192(179) = 9.94
Cb192(180) = 10.24
Cb192(181) = 10.55
Cb192(182) = 10.85
Cb192(183) = 11.15
Cb192(184) = 11.53 ' 23

Cb192(185) = 11.92
Cb192(186) = 12.3
Cb192(187) = 12.69
Cb192(188) = 13.08
Cb192(189) = 13.47
Cb192(190) = 13.85
Cb192(191) = 14.24
Cb192(192) = 14.63 ' 24

Msg = Msg & Chr$(10) & "The macro (Cband8) REPLACES the Percentile Frequency line number integers with frequency values (kHz) "
Msg = Msg & Chr$(10) & "for results from Patterns using 8 lines per band (192 lines per Pattern)[B&K].  "
Msg = Msg & Chr$(10) & "IT IS RECOMMENDED that this conversion only be done on copies of the %-ile Freq. line number results."
Msg = Msg & Chr$(10) & " "
Msg = Msg & Chr$(10) & "Do you want to continue?"

Response = MsgBox(Msg, vbYesNo, "Cband8: Conversion of Line Numbers to Frequency Values")
If Response = vbYes Then

    'Identify the Calculation Range
    startcol = Selection.Column
    numcols = Selection.Columns.Count
    startrow = Selection.Row
    numrows = Selection.Rows.Count

'Convert Line Number (Interger) to Frequency (kHz)
For R = startrow To startrow + numrows - 1
  For C = startcol To startcol + numcols - 1
    ActiveSheet.Cells(R, C).Value = Cb192(ActiveSheet.Cells(R, C).Value)
  Next C
Next R

End If

End Sub


Cband10 – an EXCEL macro to convert ArtemiS line numbers to Hz

 Sub Cband10()
   '
   'Written by: Richard J. Fridrich 06AU08
   '
   ' For %-ile Freq's from ArtemiS Data which uses 10 lines per band for a total of 240 lines per pattern
   ' Replaces line numbers (integers from 1 to 240) with frequency values (kHz)
   '
Dim Msg
Dim Cb240(240)

cb240( 1 )= 9.417342186
cb240( 2 )= 19.58385658
cb240( 3 )= 29.74574089
cb240( 4 )= 39.90169144
cb240( 5 )= 50.05033112
cb240( 6 )= 60.19025421
cb240( 7 )= 70.31999969
cb240( 8 )= 80.43810272
cb240( 9 )= 90.54311371
cb240( 10 )= 100.6335907
cb240( 11 )= 110.708168
cb240( 12 )= 120.765564
cb240( 13 )= 130.8046112
cb240( 14 )= 140.8243103
cb240( 15 )= 150.8238525
cb240( 16 )= 160.8026428
cb240( 17 )= 170.7603912
cb240( 18 )= 180.6970825
cb240( 19 )= 190.6130981
cb240( 20 )= 200.5091705
cb240( 21 )= 210.3864746
cb240( 22 )= 220.2466583
cb240( 23 )= 230.0918427
cb240( 24 )= 239.9246674
cb240( 25 )= 249.7483063
cb240( 26 )= 259.5665283
cb240( 27 )= 269.3835754
cb240( 28 )= 279.2042847
cb240( 29 )= 289.0340881
cb240( 30 )= 298.8788757
cb240( 31 )= 308.7450256
cb240( 32 )= 318.6394653
cb240( 33 )= 328.5694275
cb240( 34 )= 338.5426025
cb240( 35 )= 348.566925
cb240( 36 )= 358.6505737
cb240( 37 )= 368.8018799
cb240( 38 )= 379.0292053
cb240( 39 )= 389.3409729
cb240( 40 )= 399.7454529
cb240( 41 )= 410.2507019
cb240( 42 )= 420.8645325
cb240( 43 )= 431.5943604
cb240( 44 )= 442.4471741
cb240( 45 )= 453.4293823
cb240( 46 )= 464.5468445
cb240( 47 )= 475.804718
cb240( 48 )= 487.207428
cb240( 49 )= 498.758667
cb240( 50 )= 510.4613342
cb240( 51 )= 522.3175049
cb240( 52 )= 534.3284302
cb240( 53 )= 546.4945068
cb240( 54 )= 558.8154907
cb240( 55 )= 571.2902222
cb240( 56 )= 583.9169922
cb240( 57 )= 596.6931763
cb240( 58 )= 609.6159058
cb240( 59 )= 622.6815186
cb240( 60 )= 635.8859863
cb240( 61 )= 649.2250366
cb240( 62 )= 662.6939697
cb240( 63 )= 676.2880859
cb240( 64 )= 690.0025024
cb240( 65 )= 703.8323364
cb240( 66 )= 717.7728882
cb240( 67 )= 731.8195801
cb240( 68 )= 745.9682007
cb240( 69 )= 760.2147217
cb240( 70 )= 774.5556641
cb240( 71 )= 788.987915
cb240( 72 )= 803.5089111
cb240( 73 )= 818.1166992
cb240( 74 )= 832.8097534
cb240( 75 )= 847.5873413
cb240( 76 )= 862.4492188
cb240( 77 )= 877.395752
cb240( 78 )= 892.4280396
cb240( 79 )= 907.5476074
cb240( 80 )= 922.7567749
cb240( 81 )= 938.0582886
cb240( 82 )= 953.4554443
cb240( 83 )= 968.9520874
cb240( 84 )= 984.5525513
cb240( 85 )= 1000.261597
cb240( 86 )= 1016.084351
cb240( 87 )= 1032.026245
cb240( 88 )= 1048.093384
cb240( 89 )= 1064.291626
cb240( 90 )= 1080.627441
cb240( 91 )= 1097.107666
cb240( 92 )= 1113.738892
cb240( 93 )= 1130.52832
cb240( 94 )= 1147.48291
cb240( 95 )= 1164.610107
cb240( 96 )= 1181.917236
cb240( 97 )= 1199.411865
cb240( 98 )= 1217.101563
cb240( 99 )= 1234.994019
cb240( 100 )= 1253.096802
cb240( 101 )= 1271.417969
cb240( 102 )= 1289.96521
cb240( 103 )= 1308.746582
cb240( 104 )= 1327.77002
cb240( 105 )= 1347.043701
cb240( 106 )= 1366.575806
cb240( 107 )= 1386.374512
cb240( 108 )= 1406.448364
cb240( 109 )= 1426.805664
cb240( 110 )= 1447.455078
cb240( 111 )= 1468.405518
cb240( 112 )= 1489.665527
cb240( 113 )= 1511.244507
cb240( 114 )= 1533.151367
cb240( 115 )= 1555.39563
cb240( 116 )= 1577.986816
cb240( 117 )= 1600.934692
cb240( 118 )= 1624.249146
cb240( 119 )= 1647.94043
cb240( 120 )= 1672.018921
cb240( 121 )= 1696.495361
cb240( 122 )= 1721.380493
cb240( 123 )= 1746.685547
cb240( 124 )= 1772.421997
cb240( 125 )= 1798.60144
cb240( 126 )= 1825.23584
cb240( 127 )= 1852.337769
cb240( 128 )= 1879.919434
cb240( 129 )= 1907.994141
cb240( 130 )= 1936.574829
cb240( 131 )= 1965.675293
cb240( 132 )= 1995.309448
cb240( 133 )= 2025.491455
cb240( 134 )= 2056.236084
cb240( 135 )= 2087.558105
cb240( 136 )= 2119.473389
cb240( 137 )= 2151.997314
cb240( 138 )= 2185.146484

cb240( 139 )= 2218.9375
cb240( 140 )= 2253.386963
cb240( 141 )= 2288.512939
cb240( 142 )= 2324.333252
cb240( 143 )= 2360.866211
cb240( 144 )= 2398.130859
cb240( 145 )= 2436.14624
cb240( 146 )= 2474.932617
cb240( 147 )= 2514.510498
cb240( 148 )= 2554.900391
cb240( 149 )= 2596.124023
cb240( 150 )= 2638.203369
cb240( 151 )= 2681.160889
cb240( 152 )= 2725.02002
cb240( 153 )= 2769.804443
cb240( 154 )= 2815.53833
cb240( 155 )= 2862.246826
cb240( 156 )= 2909.955566
cb240( 157 )= 2958.690918
cb240( 158 )= 3008.479736
cb240( 159 )= 3059.349609
cb240( 160 )= 3111.329346
cb240( 161 )= 3164.447266
cb240( 162 )= 3218.733887
cb240( 163 )= 3274.219727
cb240( 164 )= 3330.935791
cb240( 165 )= 3388.914795
cb240( 166 )= 3448.189209
cb240( 167 )= 3508.793213
cb240( 168 )= 3570.761719
cb240( 169 )= 3634.129639
cb240( 170 )= 3698.934326
cb240( 171 )= 3765.212646
cb240( 172 )= 3833.003174
cb240( 173 )= 3902.345215
cb240( 174 )= 3973.279297
cb240( 175 )= 4045.84668
cb240( 176 )= 4120.089844
cb240( 177 )= 4196.052734
cb240( 178 )= 4273.779297
cb240( 179 )= 4353.31543
cb240( 180 )= 4434.708496
cb240( 181 )= 4518.006348
cb240( 182 )= 4603.258789
cb240( 183 )= 4690.515625
cb240( 184 )= 4779.829102
cb240( 185 )= 4871.25293
cb240( 186 )= 4964.84082
cb240( 187 )= 5060.648926
cb240( 188 )= 5158.734863
cb240( 189 )= 5259.157715
cb240( 190 )= 5361.977051
cb240( 191 )= 5467.254883
cb240( 192 )= 5575.054688
cb240( 193 )= 5685.441895
cb240( 194 )= 5798.482422
cb240( 195 )= 5914.244629
cb240( 196 )= 6032.798828
cb240( 197 )= 6154.216309
cb240( 198 )= 6278.571289
cb240( 199 )= 6405.939453
cb240( 200 )= 6536.396973
cb240( 201 )= 6670.024414
cb240( 202 )= 6806.901855
cb240( 203 )= 6947.11377
cb240( 204 )= 7090.744629
cb240( 205 )= 7237.882813
cb240( 206 )= 7388.617676
cb240( 207 )= 7543.041504
cb240( 208 )= 7701.248535
cb240( 209 )= 7863.335449
cb240( 210 )= 8029.401367
cb240( 211 )= 8199.547852
cb240( 212 )= 8373.879883
cb240( 213 )= 8552.50293
cb240( 214 )= 8735.527344
cb240( 215 )= 8923.06543
cb240( 216 )= 9115.231445
cb240( 217 )= 9312.144531
cb240( 218 )= 9513.924805
cb240( 219 )= 9720.696289
cb240( 220 )= 9932.585938
cb240( 221 )= 10149.72363
cb240( 222 )= 10372.24316
cb240( 223 )= 10600.28125
cb240( 224 )= 10833.97852
cb240( 225 )= 11073.47852
cb240( 226 )= 11318.92773
cb240( 227 )= 11570.47754
cb240( 228 )= 11828.2832
cb240( 229 )= 12092.50293
cb240( 230 )= 12363.29883
cb240( 231 )= 12640.83887
cb240( 232 )= 12925.29199
cb240( 233 )= 13216.83398
cb240( 234 )= 13515.64453
cb240( 235 )= 13821.9082
cb240( 236 )= 14135.81152
cb240( 237 )= 14457.54883
cb240( 238 )= 14787.31738
cb240( 239 )= 15125.32031
cb240( 240 )= 15471.7666
 
Msg = Msg & Chr$(10) & "The macro (Cband10) REPLACES the Percentile Frequency line number integers with frequency values (Hz) "
Msg = Msg & Chr$(10) & "for results from Patterns using 10 lines per band (240 lines per Pattern)[ArtemiS].  "
Msg = Msg & Chr$(10) & "IT IS RECOMMENDED that this conversion only be done on copies of the %-ile Freq. line number results."
Msg = Msg & Chr$(10) & " "
Msg = Msg & Chr$(10) & "Do you want to continue?"

Response = MsgBox(Msg, vbYesNo, "Cband10: Conversion of Line Numbers to Frequency Values")
If Response = vbYes Then

    'Identify the Calculation Range
    startcol = Selection.Column
    numcols = Selection.Columns.Count
    startrow = Selection.Row
    numrows = Selection.Rows.Count

'Convert Line Number (Interger) to Frequency (kHz)
For R = startrow To startrow + numrows - 1
  For C = startcol To startcol + numcols - 1
    ActiveSheet.Cells(R, C).Value = Cb240(ActiveSheet.Cells(R, C).Value)
  Next C
Next R

End If

End Sub