clear all
close all
%% configuration of sound card
ai = analoginput('winsound');               % set sound card to analog input
chan = addchannel(ai,1);                    % add one audio channel (Mono)
duration = 8;                              % Ten second acquisition 
set(ai,'SampleRate',5000)                   % set Sample Rate 
ActualRate1 = get(ai,'SampleRate'); 
t01=1/ActualRate1;                                              % calculating of sample time
set(ai,'SamplesPerTrigger',duration*ActualRate1) 

%% Start recording
start(ai)
[Signal, Fs]= wavread('Violin.wav');
wavplay (Signal,44100);
data1 = getdata(ai);
N1=length(data1);
t1=(0:t01:((N1*t01)-t01))';

%% Calculating  of FFT
Nfft1=2^nextpow2(N1);
f1=ActualRate1/2*linspace(0,1,1+Nfft1/2);      % create freqs vector
y1=fft(data1,Nfft1)/N1;

%% Ploting time domain
figure (1)
subplot (2,1,1)
plot(t1, data1);
axis tight
xlabel('time in second')
ylabel ('Amplitude in volt')
title('SampleRate5000')
grid on

%% Ploting frequency domain
figure (2)
subplot (2,1,1)
yplot1=abs(y1(1:1+Nfft1/2));
yplot1=yplot1/max(yplot1);
plot(f1*1e-3, yplot1);
axis([0,5,0,1]);
xlabel('kHz')
ylabel ('Amplitude')
title('SampleRate5000')
grid on

%% configuration of sound card
set(ai,'SampleRate',32000)                   % set Sample Rate 
ActualRate2 = get(ai,'SampleRate'); 
t02=1/ActualRate2;                                              % calculating of sample time
set(ai,'SamplesPerTrigger',duration*ActualRate2) 

%% Start recording with new sample rate
start(ai)
wavplay (Signal,44100);
data2 = getdata(ai);
N2=length(data2);
t2=(0:t02:((N2*t02)-t02))';
delete(ai) 
clear ai

%% Calculating and ploting of FFT
Nfft2=2^nextpow2(N2);
f2=ActualRate2/2*linspace(0,1,1+Nfft2/2);      % create freqs vector
y2=fft(data2,Nfft2)/N2;

%% ploting time domain 
figure (1)
subplot (2,1,2)
plot(t2, data2);
axis tight
xlabel('time in second')
ylabel ('Amplitude in volt')
title('SampleRate32000')
grid on

%% Ploting frequency domain
figure (2)
subplot (2,1,2)
yplot2=abs(y2(1:1+Nfft2/2));
yplot2=yplot2/max(yplot2);
plot(f2*1e-3, yplot2);
axis([0,5,0,1]);
xlabel('kHz')
ylabel ('Amplitude')
title('SampleRate32000')
grid on