1 % Outputs a file for testing purposes. 2 % 3 % Adjust the following parameters to suit. Their purpose becomes more clear on 4 % viewing the gain plots. 5 % MaxGain: Max gain in dB 6 % MinGain: Min gain at overload (0 dBov) in dB 7 % CompRatio: Compression ratio, essentially determines the slope of the gain 8 % function between the max and min gains 9 % Knee: The smoothness of the transition to max gain (smaller is smoother) 10 MaxGain = 5; MinGain = 0; CompRatio = 3; Knee = 1; 11 12 % Compute gains 13 zeros = 0:31; lvl = 2.^(1-zeros); 14 A = -10*log10(lvl) * (CompRatio - 1) / CompRatio; 15 B = MaxGain - MinGain; 16 gains = round(2^16*10.^(0.05 * (MinGain + B * ( log(exp(-Knee*A)+exp(-Knee*B)) - log(1+exp(-Knee*B)) ) / log(1/(1+exp(Knee*B)))))); 17 fprintf(1, '\t%i, %i, %i, %i,\n', gains); 18 19 % Save gains to file 20 fid = fopen('gains', 'wb'); 21 if fid == -1 22 error(sprintf('Unable to open file %s', filename)); 23 return 24 end 25 fwrite(fid, gains, 'int32'); 26 fclose(fid); 27 28 % Plotting 29 in = 10*log10(lvl); out = 20*log10(gains/65536); 30 subplot(121); plot(in, out); axis([-60, 0, -5, 30]); grid on; xlabel('Input (dB)'); ylabel('Gain (dB)'); 31 subplot(122); plot(in, in+out); axis([-60, 0, -60, 10]); grid on; xlabel('Input (dB)'); ylabel('Output (dB)'); 32 zoom on; 33