% === table_reading_correction.m === ïðîñëóõîâóâàííÿ òàáëèöü êîðåãóâàëüíå ===
%
% Ìåòà äàíî¿ ïðîãðàìè - ïðîñëóõîâóâàííÿ ïåâíèõ òàáëèöü ³ç çàäàíèìè øóìàìè
% òà ðåâåðáåðàö³éíîþ çàâàäîþ, ç ìåòîþ êîðåêö³¿ ðàí³øå îäåðæàíèõ ðåçóëüòàò³â
%
% Copyright @ À.Ïðîäåóñ, Ì.ßðîøåíêî, âåðåñåíü 2017

clc         % î÷èñòêà êîìàíäíîãî îêíà
clear all   % î÷èñòêà ïàìÿòè äàíûõ ïðîãðàììû , ò.í. workspace

nums = 50; % ê³ëüê³ñòü çâóêîñïîëó÷åíü (ìàº áóòè 50, ÷èñëî 5 - äëÿ òåñòóâàííÿ)
lastname = input('Âêàæ³òü âàøå ïð³çâèùå (áåç ëàïîê): ','s');

% 1. Ñòâîðåííÿ 5 ïàïîê äëÿ çàïèñó ðåçóëüòàò³â:
kat_rezults = ['Rezults_' lastname];   % ³ì'ÿ îñíîâíî¿ ïàïêè äëÿ ðåçóëüòàò³â
mkdir(kat_rezults);       % ñòâîðåííÿ îñíîâíî¿ ïàïêè äëÿ çàïèñó ðåçóëüòàò³â
mkdir([kat_rezults '\Group1']);% ñòâîð. ïàïêè äëÿ ðåç-ò³â 1-¿ ãðóïè äîñë³ä³â
mkdir([kat_rezults '\Group2']);% ñòâîð. ïàïêè äëÿ ðåç-ò³â 2-¿ ãðóïè äîñë³ä³â
mkdir([kat_rezults '\Group3']);% ñòâîð. ïàïêè äëÿ ðåç-ò³â 3-¿ ãðóïè äîñë³ä³â
mkdir([kat_rezults '\Group4']);% ñòâîð. ïàïêè äëÿ ðåç-ò³â 4-¿ ãðóïè äîñë³ä³â

addpath ([kat_rezults '\Group1'],[kat_rezults '\Group2'],...
    [kat_rezults '\Group3'],[kat_rezults '\Group4']);

SNRT = [-10 0 10];       % òàáëèöÿ çíà÷åíü ïîòð³áíèõ SNR0
% T60T = [0.3 0.6 0.9 1.1 1.4 2.0 2.7];   % çíà÷åííÿ ÷àñó ðåâåðáåðàö³¿

clc
disp('²ÑÍÓª ×ÎÒÈÐÈ ÃÐÓÏÈ ÄÎÑË²Ä²Â:');
disp('Ãðóïà 1 - ÷èñòèé ñèãíàë:');
disp('Ãðóïà 2 - ñèãíàë ñïîòâîðåíèé øóìîì:');
disp('Ãðóïà 3 - ñèãíàë ñïîòâîðåíèé ðåâåðáåðàö³ºþ:');
disp('Ãðóïà 4 - ñèãíàë ñïîòâîðåíèé ðåâåðáåðàö³ºþ òà øóìîì:');
disp(' ');

m = input('Âêàæ³òü íîìåð ãðóïè äîñë³ä³â:    ');
params.m = m;
disp(' ');
N = input('Âêàæ³òü íîìåð òàáëèö³:      ');
disp(' ');

params.fs = 22050;   % ÷àñòîòà äèñêðåòèçàö³¿ äîð³âíþº 22050 Ãö

% 2. Ïðèñâîºííÿ ïîòð³áíèõ çíà÷åíü òàáëèö³ ïàðàìåòð³â params:
switch m
    case 2
        ncolor = input('Ââåä³òü ÷èñëî, âêàçàíå â íàçâ³ ôàéëó ï³ñëÿ ñèìâîëó "c":    ');
        color = ['n' num2str(ncolor)];
        disp(' ');
        nsnr = input('Ââåä³òü ÷èñëî, âêàçàíå â íàçâ³ ôàéëó ï³ñëÿ ñèìâîëó "s":    ');
        params.snr0 = SNRT(nsnr); 
        noises = load('noises_table.mat');   % çàâàíòàæåííÿ òàáëèö³ øóì³â
        params.noise = noises.(color);
    case 3
        nt = input('Ââåä³òü ÷èñëî, âêàçàíå â íàçâ³ ôàéëó ï³ñëÿ ñèìâîëó "t":    ');
        rirs = load('rirs_table.mat'); % çàâàíòàæåííÿ òàáëèö³ ³ìï.õàð-ê ê³ìíàò
        fieldname = ['r' num2str(nt)];
        params.rir = rirs.(fieldname);
    case 4
        nt = input('Ââåä³òü ÷èñëî, âêàçàíå â íàçâ³ ôàéëó ï³ñëÿ ñèìâîëó "t":    ');
        rirs = load('rirs_table.mat'); % çàâàíòàæåííÿ òàáëèö³ ³ìï.õàð-ê ê³ìíàò
        fieldname = ['r' num2str(nt)];
        params.rir = rirs.(fieldname);
        disp(' ');        
        ncolor = input('Ââåä³òü ÷èñëî, âêàçàíå â íàçâ³ ôàéëó ï³ñëÿ ñèìâîëó "c":    ');
        color = ['n' num2str(ncolor)];
        disp(' ');        
        nsnr = input('Ââåä³òü ÷èñëî, âêàçàíå â íàçâ³ ôàéëó ï³ñëÿ ñèìâîëó "s":    ');
        params.snr0 = SNRT(nsnr); 
        noises = load('noises_table.mat');   % çàâàíòàæåííÿ òàáëèö³ øóì³â
        params.noise = noises.(color);
end

% 3. Âèá³ð òàáëèö³ äëÿ ïðîñëóõîâóâàííÿ
rfr1_tablename = ['rfr_table_' num2str(N) '_1.mat'];% ³ì'ÿ 1-¿ åòàëîí.òàáëèö³
rfr2_tablename = ['rfr_table_' num2str(N) '_2.mat'];% ³ì'ÿ 2-¿ åòàëîí.òàáëèö³
wav_tablename = ['wav_table_' num2str(N) '.mat'];          % ³ì'ÿ wav-òàáëèö³
wavs = load(wav_tablename);   % çàâàíòàæåííÿ wav-òàáëèö³
if m == 1                % ñóô³êñ äî íàçâè ôàéëó ðåçóëüòàò³â
    suf = ''; 
elseif m == 2
    suf = ['_c' num2str(ncolor) 's' num2str(nsnr)];
elseif m == 3
    suf = ['_t' num2str(nt)];
else
    suf = ['_t' num2str(nt) '_c' num2str(ncolor) 's' num2str(nsnr)];
end
rzlt_tablename = ['rzlt_table_' num2str(N) suf '.mat'];% ³ì'ÿ òàáëèö³ ðåçóëüòàò³â

% 4. Ïðîñëóõîâóâàííÿ çâóêîñïîëó÷åííÿ (çà íàòèñêîì äîâ³ëüíî¿ êëàâ³ø³)
clc
disp(['ÂÈ ÑËÓÕÀªÒÅ ÒÀÁËÈÖÞ ¹ ' num2str(N)]);
disp(' ');
for k = 1:nums
    inputSignal = wavs.(['w' num2str(k)]);
    [outputSignal] = distorSystem(inputSignal, params);
    soundsc(outputSignal,params.fs);
    symb = input('Ââåä³òü ïî÷óòå çâóêîñïîëó÷åííÿ áåç ëàïîê:   ','s');
    temprzlt.(['s' num2str(k)]) = symb;
    katname = [kat_rezults '\Group' num2str(m) '\' rzlt_tablename];
    save (katname, '-struct', 'temprzlt');
end

% 5. Ï³äðàõóíîê ÷àñòêè ïðàâèëüíî ñïðèéíÿòèõ çâóêîñïîëó÷åíü
rfrtbl_1 = load(rfr1_tablename); % çàâàíòàæåííÿ 1-¿ åòàëîí.òàáëèö³
rfrtbl_2 = load(rfr2_tablename); % çàâàíòàæåííÿ 2-¿ åòàëîí.òàáëèö³
num_right = 0;        % ïî÷àòêîâå çíà÷åííÿ ê³ëüêîñò³ ïðàâèëüíèõ ðåçóëüòàò³â
clc
disp('ÍÀÎ×ÍÎ ÑÏ²ÂÑÒÀÂÒÅ ÐÅÇÓËÜÒÀÒ ²Ç ÅÒÀËÎÍÀÌÈ');
disp(' ');
disp(['Åòàëîí 1 ' ' Åòàëîí 2 ' '  Ðåçóëüòàò']);
disp(' ');
for k1 = 1:nums
    fldname = ['s' num2str(k1)];
    ref1 = rfrtbl_1.(fldname);
    ref2 = rfrtbl_2.(fldname);
    rzlt = temprzlt.(fldname);
    disp([ref1 '        ' ref2 '         ' rzlt]);
    if ((strcmp(rzlt, ref1))||(strcmp(rzlt, ref2)))
        num_right = num_right + 1;
    end
end
corct = num_right/nums;
disp(' ');
disp(['Ïðàâèëüíî ñïðèéíÿòî: ' num2str(num_right) ' ³ç ' num2str(nums)]);
disp(['Îö³íêà ðîçá³ðëèâîñò³ ñòàíîâèòü: ' num2str(corct)]);

% çàïèñ â³äíîñíî¿ ê³ëüêîñò³ ïðàâèëüíèõ ðåçóëüòàò³â â òàáëèöó ðåçóëüòàò³â òà
% íà äèñê
temprzlt.('corct') = corct;
katname = [kat_rezults '\Group' num2str(m) '\' rzlt_tablename];
save (katname, '-struct', 'temprzlt');
