load noise.mat

% Put measurement data to A
A = Data(4:9,10000:30000)';

% Remove trend from data (each column)
B = detrend(A);

% Plotting of detrended data
% Note: Use plot(B(:,1)) etc. to see individual columns.
% This has just one plot so that the script doesn't spam the
% screen full of windows.
plot(B)

% Changing values to microvolts
C(:,:) = B(:,:) .* ((10^6) * 3/(2^16));

disp('********** RMS values')

% RMS in microvolts
C1 = sqrt(mean(C(:,1).^2))
C2 = sqrt(mean(C(:,2).^2))
C3 = sqrt(mean(C(:,3).^2))
C4 = sqrt(mean(C(:,4).^2))
C5 = sqrt(mean(C(:,5).^2))
C6 = sqrt(mean(C(:,6).^2))

% RMS in number of steps
C1steps = ((10^-6)*(C1))/(3/(2^16));
C2steps = ((10^-6)*(C2))/(3/(2^16));
C3steps = ((10^-6)*(C3))/(3/(2^16));
C4steps = ((10^-6)*(C4))/(3/(2^16));
C5steps = ((10^-6)*(C5))/(3/(2^16));
C6steps = ((10^-6)*(C6))/(3/(2^16));

% Finally the RMS in full bits 
C1bits = log2(C1steps)
C2bits = log2(C2steps)
C3bits = log2(C3steps)
C4bits = log2(C4steps)
C5bits = log2(C5steps)
C6bits = log2(C6steps)

% Calculate noise spectrum
% The frequency scale freq is same for all.
[NSpec(:,1), freq] = pwelch(C(:,1),[],[],[],SampleRate);
NSpec(:,2) = pwelch(C(:,2),[],[],[],SampleRate);
NSpec(:,3) = pwelch(C(:,3),[],[],[],SampleRate);
NSpec(:,4) = pwelch(C(:,4),[],[],[],SampleRate);
NSpec(:,5) = pwelch(C(:,5),[],[],[],SampleRate);
NSpec(:,6) = pwelch(C(:,6),[],[],[],SampleRate);

% Plot the noise spectrum
figure;
plot(NSpec)

disp('********** Varianssi trendittomasta signaalista mikrovoltteina')
Varsignal1 = var(C(:,1))
Varsignal2 = var(C(:,2))
Varsignal3 = var(C(:,3))
Varsignal4 = var(C(:,4))
Varsignal5 = var(C(:,5))
Varsignal6 = var(C(:,6))

disp('********** Varianssi spektrista')
% The power spectrum is µV^2 / Hz, so we must scale
% the values by the Hz per spectrum item. I.e. each
% entry in NSpec corresponds to ~1.9Hz of bandwidth.
scale = freq(2) - freq(1);
Var1 = sum(NSpec(:,1)) * scale
Var2 = sum(NSpec(:,2)) * scale
Var3 = sum(NSpec(:,3)) * scale
Var4 = sum(NSpec(:,4)) * scale
Var5 = sum(NSpec(:,5)) * scale
Var6 = sum(NSpec(:,6)) * scale

disp('********** Varianssi RMS')
RMSVar1 = C1^2
RMSVar2 = C2^2
RMSVar3 = C3^2
RMSVar4 = C4^2
RMSVar5 = C5^2
RMSVar6 = C6^2
