% Jon Newman
% 11 Feb. 2010
% Georgia Tech

% This script creates a set of FIR coefficients to be used with the FIR2 filter
% block on the RPvdsEx to make colored Gaussian waveforms with power
% preservation

%% General Parameters
% Stimulation Time Resolution (in kHz)
Fs = 24.414; 

% Coeffcient Indicies
Nf = 1:1024;

%% Specify time constants of stimulus waveforms
taus = Fs *[0.5 1 3 5 7 10 15 20 50 100]; %tau's in milliseconds. SHOULD ALWAYS BE LENGTH 10. SPECIFY WHICH ONES TO USE IN NEXT LINE.
tausUsed = [0   0 0 0 0 0  0  0  0  1  ]; % Pick the time constants you want to use during your experiment.

% create coefficients
b = zeros(length(taus),length(Nf));
taus = taus(tausUsed==1);
for j = 1:length(taus)
    bu = exp(-Nf/taus(j));
    b(j,:) = bu/norm(bu);
end

% Write the Coefficients to file
csvwrite('C:\Documents and Settings\Annihilatrix II\My Documents\Temp Data\20100323\UserFiles\Coefficient Loading\FIR_coeffs.csv',b);

%% TDT Inteface via Active X

a = csvread('C:\Documents and Settings\Annihilatrix II\My Documents\Temp Data\20100323\UserFiles\Coefficient Loading\FIR_coeffs.csv');

TD = actxcontrol('TDevAcc.X');
TD.ConnectServer('Local');
TD.SetSysMode(2)
pause(3)

% Rearrange Coeffiecients for use with FIR2
loader = [];
for i = 1:size(a,1)
    temp = zeros(1,size(a,2));
    temp(1:2:end) = a(i,1:end/2);
    temp(2:2:end) = a(i,end/2+1:end);
    loader = [loader temp];
end

%Say how many filters you are providing
TD.SetTargetVal('MEA.NumTaus',length(taus));

%Write Coefficients to memory buffer
TD.WriteTargetV('MEA.Coeffs',0,single(loader));

% Read all the coefficients
r = TD.ReadTargetV('MEA.Coeffs',0, size(loader,2));
pause(2)

TD.CloseConnection