Skip to content

Commit 7e78a37

Browse files
committed
Create MeshMapping_MatlabInterp.m
1 parent c155e23 commit 7e78a37

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
%% Tomaso Muzzu - UCL - 09/03/2021
2+
% edits by EH (March 2021).
3+
4+
%% Mesh mapping modification from Matlab
5+
% adapting meshmapping file obtained with Bonsai script.
6+
% Original is the name of the output of the Bonsai script
7+
8+
%% Variable assignment
9+
10+
inputFileName = 'Tron_MeshMap_Corrected2023-06-16T15_28_09.csv';
11+
outputFileName = 'Tron_corrected_MeshMapping_MatlabOutput.csv';
12+
DisplayDim = [1280 800]; % [horizontal_pixels, vertical_pixels]
13+
AngularSpan = [240 120]; % [azimuth_deg, elevation_deg]
14+
azimuthInterpValue = 6;
15+
elevationInterpValue = 6;
16+
plotFlag = true;
17+
18+
%% import csv file generated by Bonsai
19+
20+
[x_pix,y_pix,az_deg,el_deg] = importfile(inputFileName);
21+
22+
%% Perform interpolation
23+
% Create arrary with 5 columns as follows:
24+
% [pixel_x, pixel_y, newpixel_x, newpixel_y, brightness]
25+
MeshMapping = [x_pix,y_pix,az_deg,el_deg, ones(size(x_pix,1),1)];
26+
27+
% normalise coordinates
28+
MeshMapping = sortrows(MeshMapping,3);
29+
MeshMapping(:,3) = MeshMapping(:,3)/max(MeshMapping(:,3));
30+
MeshMapping(:,4) = MeshMapping(:,4)/max(MeshMapping(:,4));
31+
32+
if plotFlag
33+
figure
34+
subplot(121)
35+
plot(MeshMapping(:,1),MeshMapping(:,2),'or');
36+
title('Original mesh mapping file')
37+
end
38+
39+
FX = scatteredInterpolant(MeshMapping(:,3),MeshMapping(:,4),MeshMapping(:,1),'natural');
40+
FY = scatteredInterpolant(MeshMapping(:,3),MeshMapping(:,4),MeshMapping(:,2),'natural');
41+
FI = scatteredInterpolant(MeshMapping(:,3),MeshMapping(:,4),MeshMapping(:,5),'natural');
42+
43+
AzRange = linspace(0,1,AngularSpan(1)/azimuthInterpValue);
44+
ElRange = linspace(0,1,AngularSpan(2)/elevationInterpValue);
45+
[newDegAz, newDegEl ]= meshgrid(AzRange,ElRange);
46+
posX = FX(newDegAz,newDegEl);
47+
posY = FY(newDegAz,newDegEl);
48+
intensity = FI(newDegAz,newDegEl);
49+
50+
dataCell = [posX(:) -posY(:) newDegAz(:) newDegEl(:) intensity(:)];
51+
MeshNormInterp = sortrows(dataCell,3);
52+
53+
if plotFlag
54+
subplot(122)
55+
plot(MeshMapping(:,1),MeshMapping(:,2),'ro');
56+
hold on
57+
plot(MeshNormInterp(:,1),-1.*MeshNormInterp(:,2),'k.')
58+
legend({'original data', 'interpolated data'})
59+
title('Interpolated mesh map file')
60+
end
61+
62+
%% Save output file 2019a onwards
63+
%writematrix(MeshNormInterp,outputFileName)
64+
65+
%% for older vesions
66+
dlmwrite(outputFileName, MeshNormInterp, 'precision', 32)
67+
68+
69+
70+
%% function for importing csv (autogenerated using matlab)
71+
function [x_pix,y_pix,az_deg,el_deg] = importfile(filename, startRow, endRow)
72+
%IMPORTFILE Import numeric data from a text file as column vectors.
73+
% [X_PIX,Y_PIX,AZ_DEG,EL_DEG] = IMPORTFILE(FILENAME) Reads data from text
74+
% file FILENAME for the default selection.
75+
%
76+
% [X_PIX,Y_PIX,AZ_DEG,EL_DEG] = IMPORTFILE(FILENAME, STARTROW, ENDROW)
77+
% Reads data from rows STARTROW through ENDROW of text file FILENAME.
78+
%
79+
% Example:
80+
% [x_pix,y_pix,az_deg,el_deg] = importfile('MeshMap_Corrected2021-03-17T17_09_50.csv',1, 65);
81+
%
82+
% See also TEXTSCAN.
83+
84+
% Auto-generated by MATLAB on 2021/03/22 12:04:45
85+
86+
%% Initialize variables.
87+
delimiter = ',';
88+
if nargin<=2
89+
startRow = 1;
90+
endRow = inf;
91+
end
92+
93+
%% Format for each line of text:
94+
% column1: double (%f)
95+
% column2: double (%f)
96+
% column3: double (%f)
97+
% column4: double (%f)
98+
% For more information, see the TEXTSCAN documentation.
99+
formatSpec = '%f%f%f%f%[^\n\r]';
100+
101+
%% Open the text file.
102+
fileID = fopen(filename,'r');
103+
104+
%% Read columns of data according to the format.
105+
% This call is based on the structure of the file used to generate this
106+
% code. If an error occurs for a different file, try regenerating the code
107+
% from the Import Tool.
108+
dataArray = textscan(fileID, formatSpec, endRow(1)-startRow(1)+1, 'Delimiter', delimiter, 'TextType', 'string', 'HeaderLines', startRow(1)-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
109+
for block=2:length(startRow)
110+
frewind(fileID);
111+
dataArrayBlock = textscan(fileID, formatSpec, endRow(block)-startRow(block)+1, 'Delimiter', delimiter, 'TextType', 'string', 'HeaderLines', startRow(block)-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
112+
for col=1:length(dataArray)
113+
dataArray{col} = [dataArray{col};dataArrayBlock{col}];
114+
end
115+
end
116+
117+
%% Close the text file.
118+
fclose(fileID);
119+
120+
%% Post processing for unimportable data.
121+
% No unimportable data rules were applied during the import, so no post
122+
% processing code is included. To generate code which works for
123+
% unimportable data, select unimportable cells in a file and regenerate the
124+
% script.
125+
126+
%% Allocate imported array to column variable names
127+
x_pix = dataArray{:, 1};
128+
y_pix = dataArray{:, 2};
129+
az_deg = dataArray{:, 3};
130+
el_deg = dataArray{:, 4};
131+
132+
end

0 commit comments

Comments
 (0)