|
| 1 | +%% OpossumAGC Automatic gain control with hysteresis control of noisy signals |
| 2 | +% |
| 3 | +% Copyright (c) 2017-2021 Winry R. Litwa-Vulcu. All rights reserved. |
| 4 | +% |
| 5 | +% This program is free software: you can redistribute it and/or modify |
| 6 | +% it under the terms of the GNU General Public License as published by |
| 7 | +% the Free Software Foundation, either version 3 of the License, or |
| 8 | +% (at your option) any later version. |
| 9 | +% |
| 10 | +% This program is distributed in the hope that it will be useful, |
| 11 | +% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +% GNU General Public License for more details. |
| 14 | +% |
| 15 | +% You should have received a copy of the GNU General Public License |
| 16 | +% along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +clc; close all hidden; clearvars; %drawnow; pause(0.05); |
| 19 | + |
| 20 | +volume = 34; |
| 21 | +audio_level = 3452; |
| 22 | +range_mB = 600; |
| 23 | +range_step_mB = 50; |
| 24 | + |
| 25 | +noiseline = @(start, stop, len, noiselevel) rot90((randn(1, len) .* noiselevel) ... |
| 26 | + + linspace(start, stop, len), -1); |
| 27 | +segment_length = @(x) round(abs(x * randn)); |
| 28 | + |
| 29 | +raw_audio_level = ([noiseline(3000, 4000, segment_length(1001), 30) |
| 30 | + noiseline(4000, 2800, segment_length(1001), 40) |
| 31 | + noiseline(2800, 6500, segment_length(1001), 50) |
| 32 | + noiseline(6500, 7000, segment_length(1001), 45) |
| 33 | + noiseline(7000, 6300, segment_length(1001), 55) |
| 34 | + noiseline(6300, 4000, segment_length(1001), 50) |
| 35 | + noiseline(4000, 4500, segment_length(1001), 35) |
| 36 | + noiseline(4500, 1800, segment_length(1001), 30) |
| 37 | + noiseline(1800, 1700, segment_length(1001), 20) |
| 38 | + noiseline(1700, 3600, segment_length(1001), 30)]); |
| 39 | + |
| 40 | +mean_audio_level = zeros(size(raw_audio_level)); |
| 41 | +mbuff = raw_audio_level(1) * ones(1, 32); |
| 42 | +buff_indx_32 = 1; |
| 43 | +for k = 1:length(raw_audio_level) |
| 44 | + if (buff_indx_32 > 32) |
| 45 | + buff_indx_32 = 1; |
| 46 | + end |
| 47 | + if (raw_audio_level(k) > mbuff(buff_indx_32)) |
| 48 | + mbuff(buff_indx_32) = mbuff(buff_indx_32) ... |
| 49 | + + (raw_audio_level(k) - mbuff(buff_indx_32)) / 2; |
| 50 | + else |
| 51 | + mbuff(buff_indx_32) = mbuff(buff_indx_32) .* (31/32); |
| 52 | + end |
| 53 | + mean_audio_level(k) = mean(mbuff); |
| 54 | + |
| 55 | + buff_indx_32 = buff_indx_32 + 1; |
| 56 | +end |
| 57 | + |
| 58 | +dB_CoefTable = [2053, 2175, 2303, 2440, 2584; |
| 59 | + 2738, 2900, 3072, 3254, 3446; |
| 60 | + 3651, 3867, 4096, 4339, 4596; |
| 61 | + 4868, 5157, 5462, 5786, 6129; |
| 62 | + 6492, 6876, 7284, 7715, 8173]; |
| 63 | +levels_raw = bitshift(uint32(audio_level .* dB_CoefTable), -12); |
| 64 | + |
| 65 | +[v_map, a, b, C] = com.programming.OpossumVolumeMap(volume, range_mB, range_step_mB); |
| 66 | + |
| 67 | +v = b:-1:a |
| 68 | +C |
| 69 | + |
| 70 | +%VolOut_absolute_mB = fliplr(rot90(reshape(c(v_map - a + 1), 5, 5), -1)) |
| 71 | +dBFastRelativeLevel_mB = levels_raw |
| 72 | +VolOut_setting = fliplr(rot90(reshape(v_map - 1, 5, 5), -1)) |
| 73 | + |
| 74 | +% ################################################################################################## |
| 75 | +[vol, volOut] = deal(volume); |
| 76 | +DB_FAST_COEFFICIENT_COUNT = numel(dB_CoefTable); |
| 77 | +levels_raw = sort(double(levels_raw(:))); |
| 78 | + |
| 79 | + |
| 80 | +indx = 1 * ones(length(raw_audio_level), 1); |
| 81 | +indx(1) = 13; |
| 82 | +prev_indx = 1; |
| 83 | +for m = 2:length(raw_audio_level) |
| 84 | + for k=DB_FAST_COEFFICIENT_COUNT:-1:1 |
| 85 | + if (levels_raw(k) < mean_audio_level(m)) |
| 86 | + if (abs(prev_indx - (k)) > 1) |
| 87 | + indx(m) = (k); |
| 88 | + prev_indx = indx(m); |
| 89 | + break; |
| 90 | + else |
| 91 | + indx(m) = prev_indx; |
| 92 | + break; |
| 93 | + end |
| 94 | + end |
| 95 | + end |
| 96 | +end |
| 97 | + |
| 98 | +H = plotyy((1:length(raw_audio_level))./10, raw_audio_level, ... |
| 99 | + (1:length(raw_audio_level))./10, v_map(indx)); |
| 100 | +hold on; |
| 101 | +plot(H(1), (1:length(raw_audio_level))./10, mean_audio_level, 'r'); |
| 102 | +L = line([zeros(1, 25); length(raw_audio_level) .* ones(1, 25)], [levels_raw.'; levels_raw.']); |
| 103 | +set(L(13), 'lineWidth', 3); |
| 104 | +set(H(1), 'YLim', [min(levels_raw), max(levels_raw)]); |
| 105 | +set(H(2), 'YLim', [a, b]); |
| 106 | +set(H(2), 'YGrid', 'on', 'YMinorGrid', 'off', 'YTick', 1, 'YMinorTick', 'on', 'YTickMode', 'auto'); |
| 107 | +A2 = get(H(2), 'Children'); |
| 108 | +set(A2(1), 'LineWidth', 2); |
| 109 | + |
| 110 | +%figure(2); plot(indx) |
0 commit comments