1
0

clipdb.m 581 B

1234567891011121314151617181920
  1. %% [clipped] = clipdb(s,cutoff)
  2. %% Clip magnitude of s at its maximum + cutoff in dB.
  3. %% Example: clip(s,-100) makes sure the minimum magnitude
  4. %% of s is not more than 100dB below its maximum magnitude.
  5. %% If s is zero, nothing is done.
  6. %%
  7. %% https://ccrma.stanford.edu/~jos/fp2/Matlab_listing_clipdb_m.html
  8. function [clipped] = clipdb(s,cutoff)
  9. clipped = s;
  10. as = abs(s);
  11. mas = max(as(:));
  12. if mas==0, return; end
  13. if cutoff >= 0, return; end
  14. thresh = mas*10^(cutoff/20); % db to linear
  15. toosmall = find(as < thresh);
  16. clipped = s;
  17. clipped(toosmall) = thresh;
  18. end