Headphone FIR equalization using Foobar

Discussion in 'Headphones' started by ultrabike, Sep 10, 2016.

  1. ultrabike

    ultrabike Measurbator - Admin

    Staff Member Pyrate MZR
    Joined:
    Sep 25, 2015
    Likes Received:
    8,960
    Trophy Points:
    113
    Location:
    Irvine CA
    I've been meaning to do this for a long time. Managed to do it now.

    Anyhow. I decided to equalize my HD600s to performing a simple LMS algorithm. The size of the FIR selected was 2000 taps and decided to roll off above 20 kHz to avoid corrections at frequencies the driver cannot handle well anyway.

    The steps I took are as follows:

    1) Measure the HD600 cans both left and right channels and export the impulse response in .WAV format.

    2) Load the impulse response in Octave (Maltab is better but I don't have it a home).

    3) Perform LMS algorithm (2000 taps) on each of the channels. Target white (flat) response.

    4) Make sure things converge and look awesome.

    5) Save filter coefficients in .WAV format.

    6) Install and configure "Convolver" in foobar2k using the .WAV format filter coefficients.

    To measure I played white noise and measured using another computer with REW's RTA.

    Here are the results (ignore dB scaling):

    Unequalized

    uneq.png

    Equalized

    Right channel:

    right_eq_vs.png

    Left channel:

    left_eq_vs.png

    If I move the mic/cup coupling a bit I do get a bit of degradation. But not horrible and still better than unequalized. Here are three equalized measurements at slightly different positions vs non-eq HD600:

    left_eq_vs_off.png

    How it sounds with equalization?

    Much less boomy, more brutally revealing, more presence. I don't feel things went down south. It's a different signature for sure. And again, a brutally revealing signature. Good recordings sound awesome. But most of what I have is not well recorded to start with. IMO the HD600 is already a very good neutral sounding headphone relative to other pieces of shit. But it is not perfect, and it does have a little of a warm signature. Equalizing to flat takes care of this.

    I may target less flat, more tilted response next. But IMO this shows what equalization can do.
     
    Last edited: Sep 10, 2016
  2. ultrabike

    ultrabike Measurbator - Admin

    Staff Member Pyrate MZR
    Joined:
    Sep 25, 2015
    Likes Received:
    8,960
    Trophy Points:
    113
    Location:
    Irvine CA
    BTW here is the Octave code I used (did not test with Matlab):

    It expects hd600_IR_4.wav as the stereo impulse response.

    It outputs hd600_eq.wav with the stereo FIR filter coefficients.

    Code:
    % Load REW impulse responses and validate
    [h, fs, nbits] = wavread('hd600_IR_4.wav');
    H_left = freqz(h(:,1),1,length(h(:,1)));
    H_right = freqz(h(:,2),1,length(h(:,2)));
    step = (22050-5)/length(h);
    f = 5:step:22050;f=f(1:end-1);
    figure;
    subplot(2,1,1);
    semilogx(f,20*log10(abs(H_left))+90);
    grid on;
    axis([20 20000 65 110]);
    subplot(2,1,2);
    semilogx(f,20*log10(abs(H_right))+90);
    grid on;
    axis([20 20000 65 110]);
    
    % hd600 impulse response sample to learn (and invert)
    MatchSize = 3000;
    PreCursor = 100;
    MaxIndex = find(h(:,1) == max(h(:,1)));
    hn = h(MaxIndex-PreCursor:MatchSize+MaxIndex-PreCursor,:); 
    figure;
    subplot(2,1,1);
    plot(hn(:,1));
    grid on;
    axis tight;
    subplot(2,1,2);
    plot(hn(:,2));
    grid on;
    axis tight; 
    
    % Perform LMS (least-mean-squares) on each channel and derive
    % optimized filters (c)
    N = 200000;                       % iterations
    taps = 2000;                   % number of equalizer taps
    mu = [0.0001 0.00001];  % adaptation (learning) gains
    gu = 1-0.0000001;             % lossy gain
    inDly = 1000;                   % delay amount for input samples
    Main = 900;
    i = randn(2,N);                 % input samples
    o = zeros(2,N);                 % output samples
    e = zeros(2,N);                 % error
    id = zeros(2,N);             % delayed input samples
    d = zeros(2,taps);            % adaptive filter delay line
    c = zeros(2,taps);            % filter coefficients
    c(:,Main) = 1;                    % filter center tap initialization
    y = zeros(size(i));
    for k = 1:2
      y(k,:) = filter(hn(:,k),1,i(k,:)); % convolve with hd600 response (channel to equalize)
      id(k,:) = filter([zeros(1,inDly) 1], 1, i(k,:)); % delayed input
    end
    for n = 1:N
        if n < N/2                   % change adaptation gear
            mup = mu(1);
        else
            mup = mu(2);
        end
      for k = 1:2
         d(k,:) = [y(k,n) d(k,1:end-1)];         % adaptive filter delay line update
         o(k,n) = d(k,:)*c(k,:).';                     % apply adaptive filter (filtering)
         e(k,n) = o(k,n) - id(k,n);               % error
         c(k,:) = gu*c(k,:) - mup*e(k,n)*d(k,:);    % lms coefficient adaptation (learning the headphone)
      end
    end
    
    % remove high frequency corrections
    %[b, a] = besself(11, 0.95, 'z');
    [b, a] = butter(7,0.99);
    for k = 1:2
      c(k,:) = filter(b, a, c(k,:));
    end
    
    % Plot errors (check convergence)
    figure;
    for k = 1:2
      subplot(2,1,k);
      plot(e(k,:));
      grid on;
      axis tight;
    end
    
    % Plot correction filters
    figure;
    for k = 1:2
      subplot(2,1,k);
      plot(c(k,:));
      grid on;
      axis tight;
    end
    
    % Plot correction filters frequency response
    figure(1);
    for k = 1:2
      subplot(2,1,k);
      hold on;
      C = freqz(c(k,:),1,length(c(k,:)));
      step = (22050-5)/length(c(k,:));
      f = 5:step:22050;f=f(1:end-1);
      semilogx(f,20*log10(abs(C))+90,'r');
      grid on;
      axis([20 20000 65 110]);
    end
    
    % Plot predicted impulse responses
    figure;
    PlotSize = 200;
    corr = zeros(size(h));
    for k = 1:2
      subplot(2,1,k);
      corr(:,k) = filter(c(k,:), 1, h(:,k));
      plot(h(MaxIndex-PreCursor:PlotSize+MaxIndex-PreCursor,1));
      hold on;
      plot(corr(MaxIndex-PreCursor+Main:PlotSize+MaxIndex-PreCursor+Main,k),'r');
      legend('original','corrected');
      grid on;
      axis tight;
    end
    
    % Plot predicted frequency responses
    figure;
    for k = 1:2
      CORR = freqz(corr(:,k),1,length(corr(:,k)));
      step = (22050-5)/length(CORR);
      f = 5:step:22050;f=f(1:end-1);
      subplot(2,1,k);
      semilogx(f,20*log10(abs(CORR))+90,'r');
      grid on;
      axis([20 20000 65 110]);
    end
    
    % Save filter
    filt = c.';
    wavwrite(filt, fs, nbits, 'hd600_eq.wav');
    
     
  3. purr1n

    purr1n Desire for betterer is endless.

    Staff Member Pyrate BWC
    Joined:
    Sep 24, 2015
    Likes Received:
    89,778
    Trophy Points:
    113
    Location:
    Padre Island CC TX
    Target to B&K curve.
     
  4. ultrabike

    ultrabike Measurbator - Admin

    Staff Member Pyrate MZR
    Joined:
    Sep 25, 2015
    Likes Received:
    8,960
    Trophy Points:
    113
    Location:
    Irvine CA
    Will do. The easiest thing to target is flat. To target B&K I will need to generate a filter that matches it and convolve the reference signal with it.

    Not sure when I will get to it. But I will eventually.
     
  5. castleofargh

    castleofargh Acquaintance

    Joined:
    Aug 24, 2016
    Likes Received:
    28
    Trophy Points:
    18
    so you get neutral for whatever mic calibration you had right? but how do you determine that calibration as being the sound you want? be it some idea of neutral or whatever you like.

    I tried doing this once with rubber bands and a twig because I can't code or even think about manipulating an impulse response. so I used the EQ section of REW to set the curve flat(the automated stuff, we're not savages), and then I measured a digital loop including an EQ values REW had found for me(with virtual audio cable+vsthost+EQ to get in the loop). but the obvious problem is still the same, the mic calibration. that calibration I have made right now is something to look kind of like innerfidelity and headroom stuff(let's say diffuse fieldy look alike), it is not neutral to my hear and it feels like moving the problem one step further without answering it(I'm stuck there too and hope smyth realiser will enlighten me).




    embarrassing question if anybody knows: is it legal to copy(and maybe share) the EQ curve of whatever software that applies one? is there like a copyright on signature? or some BS in between law saying it's ok to do it by ear but not to copy a sine sweep or an impulse response? or that it's ok for FR but not FR and phase? IDK where that kind of stuff could start and call it copyright infringement? a manufacturer asked me not to mention how I tried to copy the EQ of their app to use system wide with viper4android. I complied because it was asked nicely, but it's been bugging me ever since.
     
  6. ultrabike

    ultrabike Measurbator - Admin

    Staff Member Pyrate MZR
    Joined:
    Sep 25, 2015
    Likes Received:
    8,960
    Trophy Points:
    113
    Location:
    Irvine CA
    I don't think you should worry that much about the mic calibration. I mean, you do, but I wouldn't worry about it that much. I did use the mic calibration that came with my mic though.

    I think one should worry a little more about the coupling of the headphone with the mic. But I feel what I have is not too far off.

    As far as copyright infringement, I dunno. Seems to me one can copyright ones random FIR coefficients. It's alright. They can have it.

    The FIR coefficients I came up with are pretty much targeted for the HD600s I have though. In fact, I have a different set for the right and the left driver. The coefficients are not universal because the response of the drivers, while pretty well match, are not exactly the same. One should expect much larger mis-matches between different headphones.
     
  7. ultrabike

    ultrabike Measurbator - Admin

    Staff Member Pyrate MZR
    Joined:
    Sep 25, 2015
    Likes Received:
    8,960
    Trophy Points:
    113
    Location:
    Irvine CA
    Applied B&K curve. I like!

    Will do measurements later. Enjoying results right now.
     
  8. Gatucho

    Gatucho New

    Joined:
    Aug 25, 2016
    Likes Received:
    4
    Trophy Points:
    8
    Location:
    México
    This is very interesting! :bow:I whish I had the equipment to measure those impulse responses!
    Could you post the plot generated in figure1.
    I'm very intrigued as how well a linear time domain LMS fitting allows an almost flat freq. response. Specially how well the high frequency resonances are flattened! This should mean that those hd600 are in fact pretty linear.

    Could this kind of linearity be accurately predicted by a low thd+imd only? Or there is something else that could mess it?
    What I would like to see is how well different headphones lend to this process. I would think that the closer to a purely linear dynamic the better the result would be.
     
  9. ultrabike

    ultrabike Measurbator - Admin

    Staff Member Pyrate MZR
    Joined:
    Sep 25, 2015
    Likes Received:
    8,960
    Trophy Points:
    113
    Location:
    Irvine CA
    @Gatucho, lemme finish this B&K business and will reply...

    So here are how things were (uncorrected):

    uncorrected.png

    And here are things after flattening and adding B&K:

    corrected_bk.png

    Overlays are next...

    Right Channel:

    right_eq_vs.png

    Left Channel:

    left_eq_vs.png

    Measurements wise the first thing to note is how much closer the flat + B&K is to the original response vs. only flat. This correlates well with what some folks term as desired or "optimum" target response. Another thing to note is that some of the depressions have been somewhat corrected. It will not be perfect due to positioning, but there is an improvement for sure.

    Listening to the corrected response is really nice (understatement). It no longer is brutally revealing as was the case with the flat target. Things are more open and actually more pleasant to my ears than uncorrected. Impact is just right and mids and treble are improved. It's pretty addicting.

    I can only say that the B&K target works and it works well with the HD600s. FB2K convolution also works.

    This is awesome.
     
  10. ultrabike

    ultrabike Measurbator - Admin

    Staff Member Pyrate MZR
    Joined:
    Sep 25, 2015
    Likes Received:
    8,960
    Trophy Points:
    113
    Location:
    Irvine CA
    Here is a zoom of the responses showing the B&K depression.

    corrected_zoom.png

    Note I'm zooming the plot to a range of 15 dB. Typically measurements are presented with a 65 to 100 dB range (35 dB) which would look like this:

    corrected_normal_zoom.png

    Again. This does not only look good. It sounds pretty good.
     
  11. Gatucho

    Gatucho New

    Joined:
    Aug 25, 2016
    Likes Received:
    4
    Trophy Points:
    8
    Location:
    México
    Really impressed, now both sides are evenly matched at HF. Does this has an effect on soundstage? Does your rig measure both sides at the same time? Or you have to change for LR measurement?
    Sorry for all the questions but I'm really intrigued.
     
  12. ultrabike

    ultrabike Measurbator - Admin

    Staff Member Pyrate MZR
    Joined:
    Sep 25, 2015
    Likes Received:
    8,960
    Trophy Points:
    113
    Location:
    Irvine CA
    Here is figure 1:

    Figure1_Oct.png

    Here is the LMS converging:

    Figure4_Oct_LMSconv.png

    Here is the BK filter:

    Figure3_Oct_BK.png

    And here are the predicted responses:

    Figure7_Oct_Pred.png

    Indeed the HD600 is a pretty linear headphone from bass to high treble. Tonality also matches B&K pretty well as evidenced above. Only low bass is a little problematic. It is also not just a linear vs non-linear problem when dealing with other headphones. It's also a position sensitivity problem. Some headphones may have some dramatic response changes with position or may exhibit severe nulls or peaks as well.

    TBH I don't have that much fancy equipment myself. Just two old laptops (one about 8 years old which I have not given away), my el-chepo Focusrite 2i2 and REW which is free. I do have a cheap mic and a shack of a measurement rig.

    To run the programs here I use Octave, which is also free, in one of my laptops (the less crappy one). All the plots and measurements used ASIO drivers. Have to bypass the Beats SW horridness in my HP laptop.
     
  13. ultrabike

    ultrabike Measurbator - Admin

    Staff Member Pyrate MZR
    Joined:
    Sep 25, 2015
    Likes Received:
    8,960
    Trophy Points:
    113
    Location:
    Irvine CA
    I don't really put too much attention to soundstage in headphones, because that's not something they can do very well (understatement here). But they do open up quite a bit after correction.

    I do have to change for LR measurements because I only have one mic. So there will be some positional stuff going on there. As it is, not too bad. Even with positional inaccuracies. Which is good.
     
  14. ultrabike

    ultrabike Measurbator - Admin

    Staff Member Pyrate MZR
    Joined:
    Sep 25, 2015
    Likes Received:
    8,960
    Trophy Points:
    113
    Location:
    Irvine CA
    BTW. The code changed a little. The predicted response and correction filter now includes the B&K curve which was not applied in the code above. I decided to apply it after the LMS to avoid moving the center tap. It can be a pain to readjust it.
     
  15. Gatucho

    Gatucho New

    Joined:
    Aug 25, 2016
    Likes Received:
    4
    Trophy Points:
    8
    Location:
    México
    That's the niceness of a flat response, you just apply the desired curve AFTERWARDS;)

    I just wonder why the cancellation didn't converge to a really high gain at very low freqs. See that even the predicted responses fall at 10hz. How you achieved this? Was this because of FIR filter length?

    I'm sold, gonna get me some gear. I bet it beats buying another TOTL headphone.
     
  16. Serious

    Serious Inquisitive Frequency Response Plot

    Pyrate BWC MZR
    Joined:
    Sep 28, 2015
    Likes Received:
    2,594
    Trophy Points:
    113
    Location:
    near Munich, Germany
    Downward sloping target such as B&K target is absolutely necessary for measurements on a flat plate coupler. This depends on the headphone though, some headphones with the driver closer to the ear or on ear headphones may need less of a slope because they interact less strongly with the ears. I have some graphs for how much different headphones and speakers interact with the ears compared to a flat plate coupler (or omni mic for speakers).
    Actually the B&K target may be too warm for my coupler for the HD600.

    EDIT: B&K target seems to be pretty accurate. Keep in mind that the red HD600 target isn't accurate from 6kHz on - I don't think a rising response is preferrable.
    Attached my own comparo of both targets. Note that this is a target response for what the FR should look like on the coupler, not the actual FR.
    Still, maybe @ultrabike can try something that looks more like the red line, but flat from 7kHz? 2db here and there can make a big difference. Pretty sure the B&K target with the HD600 on my coupler would sound too shouty for me :p. I'm not sure how well this approach would work though as we don't use the same coupler. The best approach is probably by ear against a speaker reference.
     

    Attached Files:

    Last edited: Sep 11, 2016
  17. Dreyka

    Dreyka Acquaintance

    Banned
    Joined:
    May 5, 2016
    Likes Received:
    38
    Trophy Points:
    18
    The target curves are based on an average heard rather than your specific head and therefore the frequency response you hear would not be the same as listening to speakers calibrated to the B&K curve.

    I can understand manufacturers targeting curves like this because they have to make a one size fits all headphone. It doesn't seem to be the thing for an individual to target though.

    It does show how a well behaved headphone can be sculpted into a frequency response that is far more linear than you'd get from speakers in a good room.

    http://www.innerfidelity.com/content/warren-tenbrooks-summary-head-measurements-harman

    [​IMG]
     
  18. castleofargh

    castleofargh Acquaintance

    Joined:
    Aug 24, 2016
    Likes Received:
    28
    Trophy Points:
    18
    yup, that's why I'm more worried about setting the target than ultrabike seems to be. at least when we try to really custom the sound for ourselves.




    for the soundstage question, aside from the obvious correlation between FR and psycho acoustic(like losing more high freqs with more distance...), there is the eternal case by case question of minimum phase vs linear phase. linear phase tends to keep stuff where they are in space subjectively(and FIR usually is for linear stuff), so it shouldn't mess too much with the imaging and seems like the obvious default choice when dealing with separate left and right EQ to avoid the risk of pushing mono sounds off center with ITD(you can try messing around with a minimum phase EQ like the free easyQ VST using different left and right EQ to "see" what I mean).
    unless we're deliberately trying to change an already off center sound from the headphone's phase error in channels. in which case, minimum vs minimum can sometimes compensate each other successfully.




    /!\
    for those who will try ultrabike's stuff or any convolution for that matter, captain obvious asked me to say that you usually want music at the sample rate of the impulse response, or vice versa.

    invisible banana for scale:
     
  19. ultrabike

    ultrabike Measurbator - Admin

    Staff Member Pyrate MZR
    Joined:
    Sep 25, 2015
    Likes Received:
    8,960
    Trophy Points:
    113
    Location:
    Irvine CA
    Low frequency response compensation restrictions are probably due to the FIR length indeed. But if a larger filter is used, you could also apply a high pass afterwards as well.

    As far as TOTL headphones, price does not always correlate to performance.

    What approach did you use for equalization?

    I'm worried, but not that worried because after flattening I can apply whatever I want at this point. I did like the B&K quite a bit. It turned out to mirror the stock response quite well actually, along with correcting some issues in the upper mids and treble.

    It worked really well.

    I'm not sure how the convolver plug-in in fb2k works. It may upsample if required. That said, I used the 44.1kHz sample rate approach because that's the one that yields best low end resolution. Could try other sample rates as well. The .WAV has rate parameters in it's header so the plug-in should be able to adjust itself to this.

    I also think linear phase > minimum phase.
     
  20. ultrabike

    ultrabike Measurbator - Admin

    Staff Member Pyrate MZR
    Joined:
    Sep 25, 2015
    Likes Received:
    8,960
    Trophy Points:
    113
    Location:
    Irvine CA
    The RR Standard Harman Reference and the JBL M2 Reference Curve seem way to boomy IMO.

    Based on what I've heard in the past I feel the RR1 Modified, the B&K, and the 1 dB/Octave may be more pleasant to me and to folks in general.
     

Share This Page