% file for EELE445 Lab 5 data import and analysis
%  last edit date:2/09/12avo

%Note to Students, modify this program as you see fit to the data looking
%like you want.  example: you could add a section of code to estimate the
%number of effective bits based on the SQNR you measure...



%************ read in the data file in csv format
echo off
clc
% filename = input('data file name ?','s'); %prompt for input file--  must
% %be in the matlab search path

filename = 'EELE445lab6data1.csv' %input your data file name here
data = csvread(filename); %create data matrix- (time,voltage)
tdata = data(:,1);
Vdata = data(:,2);

% tdata = 0:.001:9.999;  %alternate time vector
% y = 1*cos(2*pi*10*tdata);  %alternate signal vector
% Vnoise = 10^0  %rms noise is the std dev
% Vdata = Vnoise*randn(size(tdata)) + y ;  %noisy signal, 

ts =(tdata(length(tdata))-tdata(1))/length(tdata);  %find the sample time
fs = 1/ts;   % sample frequency
t = 0:ts:ts*(length(tdata)-1);  %set up time vector
N = length(tdata);   %length for the FFT
Fdata = fft(Vdata)/N;  %FFT of the waveform normalized
f =[0:fs/(length(Fdata)-1):fs]-fs/2;%frequency vector for FFT with fftshift
df = fs/N  %Resolution bandwidth or cell size of the FFT

Ptime = var(Vdata) %Total power of waveform-
Pfft = sum(abs(Fdata).^2) %should be same as Powerfromtime
  %Pnoise assumes white noise and no signal from 1000 to 6000 points
Pnoise = sum(abs(Fdata(1000:6000).^2))*length(Fdata)/5000  
SQNR = 10*log10((Pfft-Pnoise)/Pnoise)
Vdc = mean(Vdata)  %dc voltage of waveform
Vrms = std(Vdata)  % Rms voltage of waveform, or use sqrt(Powerfromtime)
Vpp = max(Vdata) - min(Vdata)
Pac = Ptime - Vdc^2  %mean squared value - squared mean value

clf
figure(1)
plot(t,Vdata);
xlabel('Time');
% title({' time waveform';['Total power = ',num2str(Ptime)];...
%     ['DC Voltage = ',num2str(Vdc)];['RMS Voltage = ',num2str(Vrms)];...
%     ['Vp-p = ',num2str(Vpp)]});

title({' time waveform';['Total power = ',num2str(Ptime)];...
    ['DC Voltage = ',num2str(Vdc)];['\sigma, rms = ',num2str(Vrms)];...
    ['Vp-p = ',num2str(Vpp)]});
figure(2)
plot(f,fftshift(abs(Fdata).^2));

xlabel('Frequency Hz');
ylabel(['Volts squared per ',num2str(df),' Hz RBW']);
title({['fft of time waveform  ',filename];['Total power = ',num2str(Pfft)];...
    ['noise power = ',num2str(Pnoise)];['SQNR = ',num2str(SQNR),' dB']});

figure(3)
subplot(2,1,1)
plot(f,fftshift(20*log(abs(Fdata))));
xlabel('Frequency')
ylabel(['dBVolts peak per ',num2str(df),' Hz RBW']);
title('Log Magnitude-Spectrum ')
% pause % Press akey to see the plot of the Fourier Transform derived numerically.
subplot(2,1,2)
plot((Vdata));
hold on;
stem(diff(abs(Vdata)))   %(1000:1100))))
hold off;
xlabel('point')
ylabel('Voltage step size ')
title('Quantization step size using "diff"')
