|
| 1 | +classdef stimulation_optimal < stimulation |
| 2 | +%STIMULATION_PREDEFINED - stimulation using predefined sequences |
| 3 | + |
| 4 | + properties |
| 5 | + randstream; %the random stream |
| 6 | + sequences; %sequence pool |
| 7 | + weights; %sequence weigths and occurances |
| 8 | + targetseq; %sequences assigned to targets |
| 9 | + end |
| 10 | + |
| 11 | + methods |
| 12 | + function this = stimulation_optimal(numTargets,varargin) |
| 13 | + %STIMULATION_PREDEFINED - stimulation using predefined sequences |
| 14 | + % STIMULATION_PREDIFINED(numTargets,csvfile,seed) |
| 15 | + % CSVFILE - csv file with sequences |
| 16 | + % SEED - random seed used to randomly assign sequences to targets |
| 17 | + this@stimulation(numTargets,varargin{:}); |
| 18 | + p = inputParser; |
| 19 | + p.StructExpand = true; |
| 20 | + p.KeepUnmatched = true; |
| 21 | + addParameter(p,'sequencePool','random_seq_pool.csv',@(x) ischar(x) && exist(x, 'file') == 2); |
| 22 | + addParameter(p,'sequenceWeights','random_seq_weights.csv',@(x) ischar(x) && exist(x, 'file') == 2); |
| 23 | + addParameter(p,'randomseed',1,@(x) helper.isint(x) && length(x) == 1); |
| 24 | + parse(p,varargin{:}); |
| 25 | + |
| 26 | + this.randstream = RandStream('mt19937ar','Seed',p.Results.randomseed); |
| 27 | + this.sequences = csvread(p.Results.sequencePool)'; |
| 28 | + this.weights = csvread(p.Results.sequenceWeights)'; |
| 29 | + end |
| 30 | + |
| 31 | + function setTargetSequences(this) |
| 32 | + %SETTARGETSEQUENCES - assign random sequence of the sequence pool to each target |
| 33 | + if size(this.sequences,2) > this.numTargets |
| 34 | + % get random subset if pool has more sequences as targets |
| 35 | +% tmp_weights = this.weights(:,1)./size(this.weights,1); |
| 36 | + tmp_weights = this.weights(:,1); |
| 37 | + subset = zeros(1, this.numTargets); |
| 38 | +% subset = randsample( size(this.sequences,2), this.numTargets, false, this.weights(1,:)./size(this.weights,2)); |
| 39 | + for i = 1:this.numTargets |
| 40 | + subnet_new_idx = randsample(this.randstream, size(this.sequences,2), 1, true, tmp_weights); |
| 41 | + tmp_weights(subnet_new_idx) = 0; |
| 42 | + subset(i) = subnet_new_idx; |
| 43 | + end |
| 44 | + |
| 45 | + elseif size(this.sequences,2) == this.numTargets |
| 46 | + % if number of sequences equals number of targets |
| 47 | + subset = 1:this.numTargets; |
| 48 | + else |
| 49 | + error('setTargetSequences: too few sequences in sequence pool'); |
| 50 | + end |
| 51 | + this.weights(subset,2) = this.weights(subset, 2) + 1; |
| 52 | + this.targetseq = this.sequences(:,subset); |
| 53 | + end |
| 54 | + |
| 55 | + function updateWeights(this, bitAcc) |
| 56 | + %UPDATEWEIGHTS - update weights using newest bit prediction |
| 57 | + %accuracy |
| 58 | + this.weights(:, 1) = this.weights(:, 1) .* bitAcc; |
| 59 | + end |
| 60 | + |
| 61 | + function bits = next(this,lostBits) |
| 62 | + %NEXT - returns the next bits of the sequence pool for each target |
| 63 | + %call super method |
| 64 | + next@stimulation(this,lostBits); |
| 65 | + if mod(this.stimPos-1,size(this.sequences,1))+1 == 1, this.setTargetSequences(); end |
| 66 | + bits = this.targetseq(mod(this.stimPos-1,size(this.sequences,1))+1,:); |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | +end |
| 71 | + |
0 commit comments