%----------------- Serial Communication with MATLAB -----------------
%
% Technical University of Sofia - MUSSPI lab course
% Created by Stoyan Nickolov
% Updated by Viktor Stoev
% November 2016
%------------------------------------------------
 
close all;
clear all;

% AI = analoginput('nidaq','Dev1');
% chan = addchannel(AI,1);
% duration = 3; 					%number of seconds for acquisition
% set(AI,'SampleRate',1000)
% ActualRate = get(AI,'SampleRate');
% set(AI,'SamplesPerTrigger',duration*ActualRate)
% set(AI,'TriggerType','Manual')
% blocksize = get(AI,'SamplesPerTrigger');
% Fs = ActualRate;
% start(AI);
% trigger(AI);
% wait(AI,duration + 1);
% data = getdata(AI);
% % delete(AI);
% % clear AI;
% 
% 
% f=Fs*((0:(size(data,1))/2)/(size(data,1)));
% FT = (abs(fft(data)));%
% %20*log10% FT = FT - max(FT);
% 
% figure;
% subplot(2,1,1);
% plot(data);
% title('Input signal in time domain');
% xlabel('Time,[s]');
% ylabel('Voltage, [v]');
% subplot(2,1,2);
% plot(f,FT(1:end/2+1,1));
% title('Input signal in frequency domain');
% xlabel('Frequency,[Hz]');
% ylabel('Power, [dB]');

% save('C:\Documents and Settings\nayots\Desktop\data.txt', '-ascii');
% disp('Data saved!');
% data(0)

%%
% dio = digitalio('nidaq','Dev1');
% lin = addline(dio,0:3,'in');
% lout = addline(dio,4:7,'out');
% p = addline(dio,0:7,1,'in');
% data = getvalue(lin);
% putvalue(lout,1)
% %data2 = getvalue(dio);
% delete(dio)
%% Sound Card

close all;
clear all;

% AI = analoginput('winsound');
% chan = addchannel(AI,1);
duration = 5; 					%number of seconds for acquisition
recObj = audiorecorder;

% set(AI,'SampleRate',48000)
% ActualRate = get(AI,'SampleRate');
% set(AI,'SamplesPerTrigger',duration*ActualRate)
% set(AI,'TriggerType','Manual')
% blocksize = get(AI,'SamplesPerTrigger');
% Fs = ActualRate;



for ii = 1:2
    if(ii == 1)
        display('Enter first sound after you press a key:');
        pause
        recordblocking(recObj,duration);
        data_1 = getaudiodata(recObj);
    else
        display('Enter second sound after you press a key:');
        pause
        recordblocking(recObj,duration);
        data_2 = getaudiodata(recObj);
    end
end

ActualRate = get(recObj,'SampleRate')
Fs = ActualRate;

time = [1:duration*ActualRate]*1/ActualRate; 
f = Fs*((0:(size(data_1,1))/2)/(size(data_1,1)));
FT_1 = (abs(fft(data_1)));
FT_2 = (abs(fft(data_2)));

figure;
subplot(2,2,1);
plot(time,data_1);
title('Input signal A in time domain');
xlabel('Time,[s]');
ylabel('Voltage, [v]');
subplot(2,2,2);
plot(f,FT_1(1:end/2+1,1));
title('Input signal A in frequency domain');
xlabel('Frequency,[Hz]');
ylabel('Power/Energy, [dB]/value');
subplot(2,2,3);
plot(time,data_2);
title('Input signal B in time domain');
xlabel('Time,[s]');
ylabel('Voltage, [v]');
subplot(2,2,4);
plot(f,FT_2(1:end/2+1,1));
title('Input signal B in frequency domain');
xlabel('Frequency,[Hz]');
ylabel('Power/Energy, [dB]/value');

%% Save and Load data in .txt and .mat

% save('data.txt','data','-ascii');
% loaded_data = load('data.txt');
% save('data.mat');
% load('data.mat');

%% Do xcorr of two signals

% [c,lags] = xcorr(data_1,data_2,'biased');

figure;
subplot(3,1,1)
plot(xcorr(data_1,data_2,'coeff'));
title('Xcorr of A and B');
subplot(3,1,2)
plot(xcorr(data_1,data_1,'coeff'));
title('Autocorr of A');
subplot(3,1,3)
plot(xcorr(data_2,data_2,'coeff'));
  title('Autocorr of B');