%----------------- 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 = 5; 					%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\DAQ_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
% 
% break;

close all;
clear all;

% AI = analoginput('winsound');
% chan = addchannel(AI,1);
% duration = 5; 					%number of seconds for acquisition
% set(AI,'SampleRate',48000)
% 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);


duration = 3;

recObj = audiorecorder(44100, 16, 1, 5);
disp('Start speaking.')

recordblocking(recObj, duration);
disp('End of Recording.');

data = getaudiodata(recObj);
plot(data);
title('Audio Signal (double)');

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

time = [1:duration*ActualRate]*1/ActualRate; 
f = Fs*((0:(size(data,1))/2)/(size(data,1)));
FT = 20*log10(abs(fft(data)));
FT = FT - max(FT);

figure;
subplot(2,1,1);
plot(time,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/Energy, [dB]/value');
