Commit 5d272e9b47de683acff2e89e6b53fb44b8c27107

Authored by David Mayerich
1 parent f28202b1

added matlab files

matlab/brewermap.m 0 → 100644
  1 +function result = stimBrewerColormap(R)
  2 +
  3 +%returns a Brewer colormap with the specified resolution R
  4 +
  5 +ctrlPts = zeros(11, 3);
  6 +
  7 +ctrlPts(1, :) = [0.192157, 0.211765, 0.584314];
  8 +ctrlPts(2, :) = [0.270588, 0.458824, 0.705882];
  9 +ctrlPts(3, :) = [0.454902, 0.678431, 0.819608];
  10 +ctrlPts(4, :) = [0.670588, 0.85098, 0.913725];
  11 +ctrlPts(5, :) = [0.878431, 0.952941, 0.972549];
  12 +ctrlPts(6, :) = [1, 1, 0.74902];
  13 +ctrlPts(7, :) = [0.996078, 0.878431, 0.564706];
  14 +ctrlPts(8, :) = [0.992157, 0.682353, 0.380392];
  15 +ctrlPts(9, :) = [0.956863, 0.427451, 0.262745];
  16 +ctrlPts(10, :) = [0.843137, 0.188235, 0.152941];
  17 +ctrlPts(11, :) = [0.647059, 0, 0.14902];
  18 +
  19 +X = 1:11;
  20 +
  21 +r = 1:11/R:11;
  22 +
  23 +R = interp1(X, ctrlPts(:, 1), r);
  24 +G = interp1(X, ctrlPts(:, 2), r);
  25 +B = interp1(X, ctrlPts(:, 3), r);
  26 +
  27 +result = [R' G' B'];
  28 +
... ...
matlab/enviClose.m 0 → 100644
  1 +function rtsEnviClose(enviFID)
  2 +
  3 +fclose(enviFID.fid);
0 4 \ No newline at end of file
... ...
matlab/enviID.m 0 → 100644
  1 +classdef enviID < handle
  2 + properties
  3 + fid
  4 + header
  5 + mask
  6 + fpos
  7 + end
  8 +end
0 9 \ No newline at end of file
... ...
matlab/enviLoadHeader.m 0 → 100644
  1 +function s = enviLoadHeader(filename)
  2 +%create a cell array of fields
  3 +s = struct;
  4 +
  5 +fid = fopen(filename, 'r', 'n', 'US-ASCII');
  6 +
  7 +%read the first field and make sure it is "ENVI"
  8 +fname = GetFieldName(fid);
  9 +if strcmp(fname, 'ENVI') == 0
  10 + disp('Not an ENVI header file');
  11 + return;
  12 +end
  13 +
  14 +while feof(fid) == 0
  15 + fname = GetFieldName(fid);
  16 + if feof(fid) == 1
  17 + return;
  18 + end
  19 + [value, valid] = ReadField(fid, fname);
  20 + if valid == 1
  21 + s = setfield(s, fname, value);
  22 + end
  23 +end
  24 +fclose(fid);
  25 +
  26 +function t = GetFieldName(fid)
  27 +string_struct = textscan(fid, '%s', 1, 'Delimiter', '=');
  28 +if feof(fid) == 1
  29 + t = [];
  30 + return;
  31 +end
  32 +t = string_struct{1}{1};
  33 +t = strtrim(t);
  34 +t(t==' ') = '_';
  35 +
  36 +function [v, valid] = ReadField(fid, field_name)
  37 +valid = 1;
  38 +stringFields = {'file_type', 'interleave', 'sensor_type', 'wavelength_units'};
  39 +intFields = {'samples', 'lines', 'bands', 'header_offset', 'data_type', 'byte_order'};
  40 +
  41 +%if the field is "description", read between the brackets
  42 +if strcmp(field_name, 'description') == 1
  43 + textscan(fid, '%[{]', 1);
  44 + string_struct = textscan(fid, '%[^}]', 1, 'Whitespace', '');
  45 + textscan(fid, '%[}]', 1);
  46 + v = string_struct{1}{1};
  47 + v = strtrim(v);
  48 + return;
  49 +end
  50 +if max(strcmp(field_name, intFields)) ~= 0
  51 + v = fscanf(fid, '%d');
  52 + return;
  53 +end
  54 +if max(strcmp(field_name, stringFields)) ~= 0
  55 + string_struct = textscan(fid, '%s', 1, 'Whitespace', '\n');
  56 + v = string_struct{1}{1};
  57 + v = strtrim(v);
  58 + return;
  59 +end
  60 +
  61 +%read and return the wavelength values
  62 +if strcmp(field_name, 'wavelength') == 1
  63 + v = [];
  64 + textscan(fid, '%[{]', 1);
  65 + c = ' ';
  66 + while c ~= '}'
  67 + new = fscanf(fid, '%f');
  68 + v = [v new];
  69 + c = fscanf(fid, '%c', 1);
  70 + end
  71 + return;
  72 +end
  73 +
  74 +%if it doesn't match anything, just read until the end of the line
  75 +%string_struct = textscan(fid, '%s', 1, 'Whitespace', '\n');
  76 +string_struct = textscan(fid, '%s', 1, 'Delimiter', '\n');
  77 +v = '';
  78 +valid = 0;
... ...
matlab/enviOpen.m 0 → 100644
  1 +function EID = rtsEnviOpen(filename, headername, mask)
  2 +
  3 +%opens an ENVI file and returns an ID structure
  4 +% filename = name of ENVI file
  5 +% headername = name of ENVI header
  6 +% FID = structure containing file information
  7 +% mask = binary image specifying valid spectra
  8 +
  9 +%assign the mask variable
  10 +if(nargin < 3)
  11 + mask = 1;
  12 +end
  13 +
  14 +%open the file and save the file ID
  15 +fid = fopen(filename, 'r');
  16 +
  17 +%open the ENVI header file
  18 +header = enviLoadHeader(headername);
  19 +
  20 +%this is currently only valid for BIP files
  21 +if(~strcmp(header.interleave, 'bip'))
  22 + disp('Error: This function only works for BIP interleave files. Load in ENVI and convert.');
  23 +end
  24 +if(header.data_type ~= 4)
  25 + disp('Error: This function only works for floating-point files.');
  26 +end
  27 +
  28 +EID = enviID;
  29 +EID.fid = fid;
  30 +EID.header = header;
  31 +EID.mask = mask;
  32 +EID.fpos = 0;
  33 +
  34 +%EID = struct('fid', fid, 'header', header, 'mask', mask, 'fpos', 0);
  35 +
... ...
matlab/enviRead.m 0 → 100644
  1 +function S = rtsEnviRead(fid, batchSize)
  2 +
  3 +%This function reads a batch of spectra from the given ENVI file ID
  4 +% fid = ENVI file ID created using rtsEnviOpen
  5 +% batchSize = number of spectra to read
  6 +
  7 +%if there is no mask, just load each spectrum in order
  8 +if(fid.mask == 1)
  9 +
  10 + %compute the new batch size in case we are near the eof
  11 + nSpectra = fid.header.samples * fid.header.lines;
  12 + remainingSpectra = nSpectra - fid.fpos;
  13 +
  14 + if(batchSize > remainingSpectra)
  15 + batchSize = remainingSpectra;
  16 + end
  17 +
  18 + S = fread(fid.fid, [fid.header.bands batchSize], '*float32');
  19 +
  20 + %increment the fid counter
  21 + fid.fpos = fid.fpos + batchSize;
  22 +
  23 +%otherwise only load valid spectra
  24 +else
  25 +
  26 + %compute the new batch size in case we are near the eof
  27 + if(fid.fpos == 0)
  28 + remainingSpectra = nnz(fid.mask);
  29 + else
  30 + nSpectra = nnz(fid.mask);
  31 + maskRead = fid.mask(1:fid.fpos+1);
  32 + remainingSpectra = nSpectra - nnz(maskRead);
  33 + end
  34 +
  35 + if(batchSize > remainingSpectra)
  36 + batchSize = remainingSpectra;
  37 + end
  38 +
  39 + %allocate space for the spectra
  40 + S = zeros(fid.header.bands, batchSize);
  41 +
  42 + %for each spectrum in the batch
  43 + for s = 1:batchSize
  44 +
  45 + %while the current spectrum is invalid
  46 + skip = 0;
  47 + while (~fid.mask(fid.fpos + 1))
  48 + %read the invalid spectrum
  49 + %invalid = fread(fid.fid, [fid.header.bands 1], '*float32');
  50 + skip = skip + 1;
  51 +
  52 + %increment the file position
  53 + fid.fpos = fid.fpos + 1;
  54 + end
  55 + fseek(fid.fid, skip * fid.header.bands * 4, 'cof');
  56 +
  57 + test = fread(fid.fid, [fid.header.bands 1], '*float32');
  58 + if size(test) ~= size(S(:, s))
  59 + size(test)
  60 + size(S(:, s))
  61 + end
  62 + S(:, s) = test;
  63 + fid.fpos = fid.fpos + 1;
  64 +
  65 + end
  66 +
  67 +end
  68 +
... ...