-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathTSeries.m
1700 lines (1607 loc) · 60.9 KB
/
TSeries.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
classdef TSeries
%TSeries Generic time dependent variable
% Time varibale has 2 fields: T - time [GenericTimeArray] and DATA
%
% TS = TSeries(T,DATA,[ARGS])
%
% ARGS:
% 'to','TensorOrder' - 0 (scalar-default), 1 (vector), 2 (tensor)
% 'tb','TensorBasis' -
% xyz (Cartesian)
% rtp (Spherical,colatitude)
% rlp (Spherical,latitude)
% rpz (Cylindrical)
% xy (Cartesian 2D)
% tp (Polar 2D)
% 'repres' - Representation for each of the dimensions of the DATA
% as a cell array containing description of tensor dimensionality
% or {} if the DATA dimension correspond to variables associated
% with other independent variables of the data product, such and a
% particle energy or a spectral frequency channel.
%
% For more info on representation see Cluster Metadata Dictionary
% https://caa.esac.esa.int/caa/doc_format_meta.xml
%
% ARGS MACROs:
% 'vec_xyz','vec_rtp','vec_rlp','vec_rpz' - 3D vectors
% 'vec_xy','vec_rp' - 2D vectors
%
% Example:
%
% epoch = EpochUnix(iso2epoch('2002-03-04T09:30:00Z')+(0:3));
% data4x2 = [1 2; 3 4; 5 6; 7 8];
% data4x3 = [1 2 3; 4 5 6; 7 8 9; 10 11 12];
% data4x3x3 = rand(4,3,3);
%
% % TensorOrder=1, 2 components of 2D vector
% TsVec2DXY = TSeries(epoch,data4x2,'TensorOrder',1,'TensorBasis','xy',...
% 'repres',{'x','y'})
%
% % Or using equvivalent macro
% TsVec2DXY = TSeries(epoch,data4x2,'vec_xy')
%
% % TensorOrder=1, 3 components of 3D vector
% TsVec3DRTP = TSeries(epoch,data4x3,'vec_rtp')
%
% % TensorOrder=1, 2 components (x,z) of incomplete 3D vector
% TsVec3DXZ = TSeries(epoch,data4x2,'TensorOrder',1,'TensorBasis','xyz',...
% 'repres',{'x','z'})
%
% % TensorOrder=2, 3x3 components of tensor (3D, default basis=xyz)
% TsTen3D = TSeries(epoch,data4x3x3,'TensorOrder',2,...
% 'repres',{'x','y','z'},'repres',{'x','y','z'})
%
% % TensorOrder=1, 3 energies x 3 components of vector (3D, spherical)
% TsPflux = TSeries(epoch,data4x3x3,'TensorOrder',1,'TensorBasis','rtp',...
% 'repres',{},'repres',{'r','t','p'})
% ----------------------------------------------------------------------------
% SPDX-License-Identifier: Beerware
% "THE BEER-WARE LICENSE" (Revision 42):
% <[email protected]> wrote this file. As long as you retain this notice you
% can do whatever you want with this stuff. If we meet some day, and you think
% this stuff is worth it, you can buy me a beer in return. Yuri Khotyaintsev
% ----------------------------------------------------------------------------
properties (Access=protected)
coordinateSystem_ = '';
data_
t_ % GenericTimeArray
fullDim_
tensorOrder_ = 0;
tensorBasis_ = '';
end
properties (Dependent = true)
data
time
coordinateSystem
end
properties (SetAccess = immutable,Dependent = true)
tensorOrder
tensorBasis
end
properties (Constant = true, Hidden = true)
MAX_TENSOR_ORDER = 2;
BASIS = {'xyz','rtp','rlp','rpz','xy','rp'};
BASIS_NAMES = {...
'Cartesian','Spherical,colatitude', 'Spherical,latitude','Cylindrical',...
'Cartesian 2D','Polar 2D'};
end
properties (SetAccess = protected)
representation
end
properties
name = '';
units = '';
siConversion = '';
userData = [];
end
methods
function obj = TSeries(t,data,varargin)
if nargin == 0, obj.data_ = []; obj.t_ = []; return, end
if nargin == 1 && isempty(t), obj.data_ = []; obj.t_ = []; return, end
if nargin == 2 && or(isempty(t),isempty(data)), obj.data_ = []; obj.t_ = []; return, end
if nargin<2, error('2 inputs required'), end
if ~isa(t,'GenericTimeArray')
error('irf:TSeries:TSeries:badInputs',...
'T must be of TSeries type or derived from it')
end
if ~isnumeric(data)
error('irf:TSeries:TSeries:badInputs',...
'DATA must be numeric')
end
if size(data,1) ~= t.length()
error('irf:TSeries:TSeries:badInputs',...
'T and DATA must have the same number of records')
end
obj.data_ = data; obj.t_ = t;
obj.fullDim_ = cell(ndims(data)-1,1); % time should not be included -> -1
obj.representation = cell(ndims(data)-1,1); iDim = 1; % time should not be included -> -1
%obj.representation = cell(ndims(data),1); iDim = 1;
% Why is the dimension of represenation set already here? When
% picking out a component of a Tensor: T.xy, the data becomes 2D
% (time x T.xy), but the representation should have two 'dimensions':
% 'x' and 'y'. This can be solved by resetting representation when
% specifying tensor order, since tensor order hase to be specified
% before settin representation.
% remove since time should not be included
%if ~isempty(obj.t_)
% obj.representation{iDim} = obj.t_([]); iDim = iDim + 1;
%end
args = varargin;
flagTensorOrderSet = false; flagTensorBasisSet = false;
while ~isempty(args)
x = args{1}; args(1) = [];
switch lower(x)
case {'metadata_from'}
if ~isempty(args), dobjRef = args{1}; args(1) = [];
else
error('irf:TSeries:TSeries:badInputs',...
'metadata_from requires a second argument')
end
obj = dobjRef;
obj.data_ = data; obj.t_ = t;
case {'tensor_xyz'}
if ndims(obj.data_)>3 %#ok<ISMAT>
error('irf:TSeries:TSeries:badInputs',...
'DATA has more than 3 dimensions (needed for Tensor with order 2)')
elseif size(obj.data_,2)~=3
error('irf:TSeries:TSeries:badInputs',...
'size(DATA,2) must be 3 for xyz tensor')
elseif size(obj.data_,3)~=3
error('irf:TSeries:TSeries:badInputs',...
'size(DATA,3) must be 3 for xyz tensor')
end
obj.tensorOrder_ = 2; flagTensorOrderSet = true;
[~,iB] = intersect(obj.BASIS,x(8:10));
obj.tensorBasis_ = iB; flagTensorBasisSet = true;
obj.representation{1} = {x(8), x(9), x(10)}; % 1st dimension, (rows for 2D)
obj.fullDim_{1} = true;
obj.representation{2} = {x(8), x(9), x(10)}; % 2nd dimension, (cols for 2D)
obj.fullDim_{2} = true;
case {'vec_xyz','vec_rtp','vec_rlp','vec_rpz'}
if ndims(obj.data_)>2 %#ok<ISMAT>
error('irf:TSeries:TSeries:badInputs',...
'DATA has more than 2 dimensions (needed for 3D vec)')
elseif size(obj.data_,2)~=3
error('irf:TSeries:TSeries:badInputs',...
'size(DATA,2) must be 3 for 3D vec')
end
obj.tensorOrder_ = 1; flagTensorOrderSet = true;
[~,iB] = intersect(obj.BASIS,x(5:7));
obj.tensorBasis_ = iB; flagTensorBasisSet = true;
obj.representation{1} = {x(5), x(6), x(7)};
obj.fullDim_{1} = true;
case {'vec_xy','vec_rp'}
if ndims(obj.data_)>2 %#ok<ISMAT>
error('irf:TSeries:TSeries:badInputs',...
'DATA has more than 2 dimentions (needed for 2D vec)')
elseif size(obj.data_,2)~=2
error('irf:TSeries:TSeries:badInputs',...
'size(DATA,2) must be 2 for 2D vec')
end
obj.tensorOrder_ = 1; flagTensorOrderSet = true;
[~,iB] = intersect(obj.BASIS,x(5:6));
obj.tensorBasis_ = iB; flagTensorBasisSet = true;
obj.representation{1} = {x(5), x(6)}; obj.fullDim_{1} = true;
case {'to','tensororder'}
if flagTensorOrderSet
error('irf:TSeries:TSeries:badInputs',...
'tensorOrder already set')
end
if ~isempty(args), y = args{1}; args(1) = [];
else
error('irf:TSeries:TSeries:badInputs',...
'tensorOrder requires a second argument')
end
if ~isempty(intersect(y,(0:1:obj.MAX_TENSOR_ORDER)))
obj.tensorOrder_ = y; flagTensorOrderSet = true;
if y>0
% Check if we have any data dimension with 1..3 elements
found = false;
for i=2:ndims(obj.data_)
if size(obj.data_,i)<=3, found = true; break, end
end
if ~found
error('irf:TSeries:TSeries:badInputs',...
'cannot construct tensor:all data dimensions have size<=3')
end
obj.tensorBasis_ = 1; % set default basis to XYZ
end
else
error('irf:TSeries:TSeries:badInputs',...
'tensorOrder must be 0<=tensorOrder<=%d',...
obj.MAX_TENSOR_ORDER)
end
obj.representation = cell(obj.tensorOrder,1);
iDim = 1;
%if ~isempty(obj.t_)
% obj.representation{iDim} = obj.t_; iDim = iDim + 1;
%end
case {'tb','basis','tensorbasis'}
if flagTensorBasisSet
error('irf:TSeries:TSeries:badInputs',...
'tensorBasis already set')
end
if obj.tensorOrder_==0
error('irf:TSeries:TSeries:badInputs',...
'cannot set tensorBasis for a scalar (tensorOrder=0)')
end
if ~isempty(args), y = args{1}; args(1) = [];
else
error('irf:TSeries:TSeries:badInputs',...
'tensorBasis requires a second argument')
end
[~,iB] = intersect(obj.BASIS,y);
if ~isempty(iB)
obj.tensorBasis_ = iB; flagTensorBasisSet = true;
else
error('irf:TSeries:TSeries:badInputs',...
'tensorBasis value not recognized')
end
case {'rep','repres','representation'}
if isempty(obj.tensorOrder_)
error('irf:TSeries:TSeries:badInputs',...
'Must specify TensorOrder first')
end
if iDim>(obj.tensorOrder) %ndims(data), % remove '+1' after obj.tensorOrder, since time should no longer be included
error('irf:TSeries:TSeries:badInputs',...
'Representation already set for all DATA dimensions')
end
if ~isempty(args), y = args{1}; args(1) = [];
else
error('irf:TSeries:TSeries:badInputs',...
'Representation requires a second argument')
end
if isempty(y) && iDim<ndims(data)-1, iDim = iDim + 1;
else
[ok,msg] = validate_representation(y);
if ~isempty(ok)
obj.fullDim_{iDim}=ok; obj.representation{iDim} = y;
iDim = iDim + 1;
else
error('irf:TSeries:TSeries:badInputs',msg)
end
end
case 'depend'
otherwise
error('irf:TSeries:TSeries:badInputs',...
'Unknown parameter')
end
end
% XXX: TODO Validate if we have non-empty Representation set for
% number of dimensions corresponding to tensor order
function [ok,msg] = validate_representation(x)
ok = []; msg = '';
if isempty(x), ok = true; return; end % if not specified return
sDim = size(obj.data,iDim+1); tb = obj.BASIS{obj.tensorBasis_};
if sDim>length(tb)
msg = sprintf(...
'Dimension %d size %d>%d (Basis=%s) cannot have Representation. Use Depend instead',...
iDim,sDim,length(tb),tb);
return
end
if ~iscell(x)
msg = 'Representation requires a cell input'; return
end
if sDim>0 && sDim~=length(x)
msg = sprintf('Representation requires a cell(%d) input',sDim);
return
end
if ~all(cellfun(@(x) ischar(x) && isscalar(x),x))
msg = sprintf(...
'Representation requires char(%d) cells elements',...
obj.tensorOrder_);
return
end
s = sprintf('%s',char(x)');
if length(s)~=length(unique(s))
msg = sprintf(...
'Representation (%s) contains repeating attributes',s);
return
end
s = unique(s);
c = length((intersect(tb,s)));
if c == 0, msg = sprintf('Unrecognized representation');
elseif length(s) == c, ok = true; % complete dim
elseif length(s) < c, ok = false; % incomplete dim
elseif length(s) > c
d = setdiff(s,tb); s1 = sprintf('%s',char(d)');
msg = sprintf(...
'Unrecognized entries (%s) for representation ''%s''',s1,tb);
else, error('should not be here')
end
end
end
function [varargout] = subsref(obj,idx)
%SUBSREF handle indexing
switch idx(1).type
% Use the built-in subsref for dot notation
case '.'
[varargout{1:nargout}] = builtin('subsref',obj,idx);
case '()'
tmpEpoch = builtin('subsref',obj.time,idx(1));
obj.t_ = tmpEpoch;
idxTmp = repmat({':'}, ndims(obj.data), 1);
idxTmp(1) = idx(1).subs;
obj.data_ = obj.data_(idxTmp{:});
if numel(idx) > 1
obj = builtin('subsref',obj,idx(2:end));
end
[varargout{1:nargout}] = obj;
case '{}'
error('irf:TSeries:subsref',...
'Not a supported subscripted reference')
end
end
function value = get.coordinateSystem(obj)
value = obj.coordinateSystem_;
end
function value = get.data(obj)
value = obj.data_;
end
function value = get.time(obj)
value = obj.t_;
end
function value = basis(obj)
value = obj.BASIS{obj.tensorBasis_};
end
function value = get.tensorBasis(obj)
value = [obj.BASIS{obj.tensorBasis_}...
' (' obj.BASIS_NAMES{obj.tensorBasis_} ')'];
end
function value = get.tensorOrder(obj)
value = obj.tensorOrder_;
end
%Components
function y = x(obj)
%access X component
[y,ok] = getComponent(obj,'x'); if ~ok, error('cannot get X'), end
end
function y = y(obj)
%access Y component
[y,ok] = getComponent(obj,'y'); if ~ok, error('cannot get Y'), end
end
function y = z(obj)
%access Z component
[y,ok] = getComponent(obj,'z'); if ~ok, error('cannot get Z'), end
end
function y = r(obj)
%access R component
[y,ok] = getComponent(obj,'r'); if ~ok, error('cannot get R'), end
end
function y = theta(obj)
%access T(theta) component
[y,ok] = getComponent(obj,'t'); if ~ok, error('cannot get THETA'), end
end
function y = phi(obj)
%access P(phi) component
[y,ok] = getComponent(obj,'p'); if ~ok, error('cannot get PHI'), end
end
function y = lambda(obj)
%access L(lambda) component
[y,ok] = getComponent(obj,'l'); if ~ok, error('cannot get LAMBDA'), end
end
function y = xx(obj)
%access X component
[y,ok] = getComponent(obj,'xx'); if ~ok, error('cannot get XX'), end
end
function y = xy(obj)
%access X component
[y,ok] = getComponent(obj,'xy'); if ~ok, error('cannot get XY'), end
end
function y = xz(obj)
%access X component
[y,ok] = getComponent(obj,'xz'); if ~ok, error('cannot get XZ'), end
end
function y = yx(obj)
%access X component
[y,ok] = getComponent(obj,'yx'); if ~ok, error('cannot get YX'), end
end
function y = yy(obj)
%access X component
[y,ok] = getComponent(obj,'yy'); if ~ok, error('cannot get YY'), end
end
function y = yz(obj)
%access X component
[y,ok] = getComponent(obj,'yz'); if ~ok, error('cannot get YZ'), end
end
function y = zx(obj)
%access X component
[y,ok] = getComponent(obj,'zx'); if ~ok, error('cannot get ZX'), end
end
function y = zy(obj)
%access X component
[y,ok] = getComponent(obj,'zy'); if ~ok, error('cannot get ZX'), end
end
function y = zz(obj)
%access X component
[y,ok] = getComponent(obj,'zz'); if ~ok, error('cannot get ZZ'), end
end
function obj = set.coordinateSystem(obj,value)
if obj.tensorOrder_ < 1
error('irf:TSeries:setcoordinateSystem:badInputs',...
'coordinateSystem can only be set for a tensor')
end
if ~ischar(value)
error('irf:TSeries:setcoordinateSystem:badInputs',...
'expecting string input')
end
obj.coordinateSystem_ = value;
end
function obj = set.data(obj,value)
if all(size(value) == size(obj.data_)), obj.data_ = value;
else
error('irf:TSeries:setdata:badInputs',...
'size of DATA cannot be changed')
end
end
function obj = set.time(obj,value)
if ~isa(value,'GenericTimeArray')
error('irf:TSeries:sett:badInputs',...
'T must be of GenericTimeArray type or derived from it')
end
if value.length() ~= obj.length()
error('irf:TSeries:sett:badInputs',...
'T must have the same number of records')
end
obj.t_ = value;
end
function res = isempty(obj)
res = isempty(obj.t_);
end
function Ts = clone(obj,t,data)
% CLONE TSeries.CLONE(t,data)
% Clones a TSeries, but allows to add new time and data.
if size(data,1) ~= t.length()
error('irf:TSeries:TSeries:badInputs',...
'T and DATA must have the same number of records')
end
Ts = obj;
Ts.data_ = data;
Ts.t_ = t;
end
function Ts = norm(obj)
% NORM Make into unit vector.
% Ts = Ts/Ts.abs;
%
% Example:
% B = B.resample(E);
% irf_plot({B,... % B xyz
% B.norm,... % B xyz norm
% E,... % E xyz
% E.dot(B.norm),... % E par
% E.dot(B.norm)*B.norm,... % E par xyz
% E - E.dot(B.norm)*B.norm}) % E perp xyz
%
if obj.tensorOrder~=1
error('Only tensororder = 1 implemented');
end
Ts = obj/obj.abs;
end
function Ts = sqrt(obj)
%SQRT Square root
if obj.tensorOrder~=0, error('Square root requires tensorOrder = 0'); end
obj.data_ = sqrt(obj.data); Ts = obj;
Ts.tensorBasis_ = ''; Ts.representation{2} = [];
if ~isempty(obj.name), Ts.name = sprintf('sqrt(%s)',obj.name); end
end
function Ts = cumsum(obj,option)
%CUMSUM Cumsum
% TS = TSeries.cumsum(option)
% option = 't' - cumsum over time
% n - integer - cumsum over data dimension n
if obj.tensorOrder~=0, error('Cumsum requires tensorOrder = 0'); end
if not(exist('option'))
option = 1;
end
switch option
case 't'
obj.data_ = cumsum(obj.data,1); Ts = obj;
otherwise % dimension is numeric value
%if option> ndims(obj.data), error('cumsum dimension exceeds data dimension'); end
if option>0
obj.data_ = cumsum(obj.data,abs(option)); Ts = obj;
elseif option<0
obj.data_ = cumsum(obj.data(:,end:-1:1),abs(option)); Ts = obj;
end
end
Ts.tensorBasis_ = ''; Ts.representation{2} = [];
if ~isempty(obj.name), Ts.name = sprintf('cumsum(%s)',obj.name); end
end
function Ts = abs(obj)
%ABS Magnitude
if obj.tensorOrder==0
Tmpdata = abs(obj.data);
elseif obj.tensorOrder==1
switch obj.basis
case {'xy','xyz'}, Tmpdata = sqrt( sum(abs(obj.data).^2, 2) );
case {'rtp','rlp','rp'}, Tmpdata = abs(obj.r.data);
case 'rpz' % cylindrical
Tmpdata = sqrt(abs(obj.r.data).^2 + abs(obj.z.data).^2);
otherwise
error('Unknown representation'); % should not be here
end
obj.representation{2} = [];
else
error('Not yet implemented');
end
obj.data_ = Tmpdata; Ts = obj;
Ts.tensorOrder_=0; Ts.tensorBasis_ = ''; %Ts.representation{2} = []; % moved this to tensorOrder = 1
if ~isempty(obj.name), Ts.name = sprintf('|%s|',obj.name); end
end
function Ts = abs2(obj)
%ABS2 Magnitude squared
if obj.tensorOrder~=1, error('Not yet implemented'); end
switch obj.basis
case {'xy','xyz'}, Tmpdata = sum(abs(obj.data).^2, 2);
case {'rtp','rlp','rp'}, Tmpdata = abs(obj.r.data).^2;
case 'rpz' % cylindrical
Tmpdata = abs(obj.r.data).^2 + abs(obj.z.data).^2;
otherwise
error('Unknown representation'); % should not be here
end
obj.data_ = Tmpdata; Ts = obj;
Ts.tensorOrder_=0; Ts.tensorBasis_ = ''; Ts.representation{2} = [];
if ~isempty(obj.name), Ts.name = sprintf('|%s|^2',obj.name); end
if ~isempty(obj.units), Ts.units = sprintf('(%s)^2',obj.units); end
end
function Ts = cross(obj,obj1)
%CROSS cross product
if ~isa(obj,'TSeries') || ~isa(obj1,'TSeries')
error('Both imputs must be TSeries');
elseif obj.tensorOrder~=1 || obj1.tensorOrder~=1
error('Only scalars and vectors are supported');
elseif obj.time~=obj1.time
error('Input TS objects have different timelines, use resample()')
end
Ts = obj;
vector_product()
update_name_units()
function vector_product()
switch obj.basis
case {'xy','rp'}
Ts = obj.transform('xy'); d1 = Ts.data;
d2 = obj1.transform('xy').data;
Ts.data_ = d1(:,1).*d2(:,2) - d1(:,2).*d2(:,1);
case {'xyz','rtp','rlp','rpz'}
Ts = obj.transform('xyz'); d1 = Ts.data;
d2 = obj1.transform('xyz').data;
Ts.data_=[ d1(:,2).*d2(:,3)-d1(:,3).*d2(:,2), ...
- (d1(:,1).*d2(:,3)-d1(:,3).*d2(:,1)), ...
d1(:,1).*d2(:,2)-d1(:,2).*d2(:,1)];
otherwise
error('Unknown representation'); % should not be here
end
end
function update_name_units()
if ~isempty(obj.name) || ~isempty(obj1.name)
if isempty(obj.name), s = 'untitled';
else, s = obj.name;
end
if isempty(obj1.name), s1 = 'untitled';
else, s1 = obj1.name;
end
Ts.name = sprintf('cross(%s,%s)',s,s1);
end
if ~isempty(obj.units) || ~isempty(obj1.units)
if isempty(obj.units), Ts.units = obj1.units;
elseif isempty(obj1.units), Ts.units = obj.units;
else, Ts.units = [obj.units ' ' obj1.units];
end
end
end
end
function l = length(obj)
% LENGTH - number of data points in time series
%
% LENGTH(TS) - number of data points in TS
if isempty(obj.t_), l = 0;
else, l = obj.t_.length();
end
end
function l = datasize(obj,option_time)
% DATASIZE - size of data
%
% TS.DATASIZE - size(TS.data):
% 1x1 scalar -> [nTimes 1]
% 1x3 vector -> [nTimes 3]
% 3x3 tensor -> [nTimes 3 3]
% TS.DATASIZE('dataonly') - remove leading index from time
% 1x1 scalar -> [1 1]
% 1x3 vector -> [1 3]
% 3x3 tensor -> [3 3]
% TS.DATASIZE('time=1') - include leading 1
% 1x1 scalar -> [1 1 1]
% 1x3 vector -> [1 1 3]
% 3x3 tensor -> [1 3 3]
if isempty(obj.t_)
l = 0;
elseif nargin == 1
l = size(obj.data);
elseif nargin>1 % option given
switch option_time
case 'dataonly'
l = size(obj(1).data);
if obj.tensorOrder == 2
l(1) = [];
elseif obj.tensorOrder == 1
l(1) = 1;
elseif length(l)>2
l(1) = [];
elseif length(l) == 2
l(1) = 1;
else
error('Option not covered.')
end
case 'time=1'
l = obj.datasize('dataonly');
l = [1 l];
otherwise
error('Do not understand input.')
end
end
end
function Ts = plus(obj,obj1)
% PLUS Addition of TS with TS/scalars.
%
% TS.PLUS(TS)
% TS + TS
%
% TS.PLUS(constant)
% TS + constant
% - Constant can be also an object of the same size
% as each data sample.
%
% Examples:
% TS3 = TS1 + TS2;
% TS2 = TS1 + 0.1;
% TS2 = TS1 + [1 2 4]; % if TS1,TS2 are vector time series
[ST,~] = dbstack; % see if plus() was called from within minus()
if numel(ST)>1 && strcmp(ST(2).name,'TSeries.minus')
operationStr = 'Minus'; operationSymbol = '-';
else, operationStr = 'Plus'; operationSymbol = '+';
end
if isnumeric(obj1) % then obj is TSeries
Ts = obj;
if isempty(obj) || isempty(obj1)
sz = size(Ts.data_); sz(1) = 0; Ts.data_ = zeros(sz);
Ts.t_ = Ts.t_([]);
else
if numel(obj1) == 1
Ts.data_ = Ts.data_ + obj1;
else
sizeInp = size(obj1);
sizeObj = size(Ts.data);
if isequal(sizeInp,sizeObj)
Ts.data_ = Ts.data_ + obj1;
elseif numel(sizeInp) == numel(sizeObj) ... % same dimensions
&& sizeInp(1) == 1 ... % one row in obj1
&& all(sizeInp(2:end) == sizeObj(2:end)) % otherwise same number of elements
Ts.data_ = Ts.data_ + repmat(obj1,[sizeObj(1) ones(1,numel(sizeInp)-1)]);
else
error([operationStr ' not defined']);
end
end
end
elseif isnumeric(obj) % then obj1 is a TSeries
Ts = obj1;
if isempty(obj1) || isempty(obj)
sz = size(Ts.data_); sz(1) = 0; Ts.data_ = zeros(sz);
Ts.t_ = Ts.t_([]);
else
if numel(obj) == 1
Ts.data_ = Ts.data_ + obj;
else
sizeInp = size(obj);
sizeObj = size(Ts.data);
if isequal(sizeInp,sizeObj)
Ts.data_ = Ts.data_ + obj;
elseif numel(sizeInp) == numel(sizeObj) ... % same dimensions
&& sizeInp(1) == 1 ... % one row in obj1
&& all(sizeInp(2:end) == sizeObj(2:end)) % otherwise same number of elements
Ts.data_ = Ts.data_ + repmat(obj,[sizeObj(1) ones(1,numel(sizeInp)-1)]);
else
error([operationStr ' not defined']);
end
end
end
elseif isa(obj,'TSeries') && isa(obj1,'TSeries')
if obj.tensorOrder~=obj1.tensorOrder
error('Inputs must have the same tensor order');
%elseif obj.tensorBasis_ ~= obj1.tensorBasis_
% error('Inputs must have the same tensor basis, use transform()');
elseif ~strcmpi(obj.units,obj1.units)
warning('Inputs do not have the same units')
end
Ts = obj;
if isempty(obj) || isempty(obj1)
sz = size(Ts.data_); sz(1) = 0; Ts.data_ = zeros(sz);
Ts.t_ = Ts.t_([]);
else
if obj.time~=obj1.time
error('Input TS objects have different timelines, use resample()')
end
Ts.data_ = obj.data + obj1.data;
end
update_name()
else
error([operationStr ' not defined']);
end
function update_name()
if ~isempty(obj.name) || ~isempty(obj1.name)
if isempty(obj.name), s = 'untitled';
else, s = obj.name;
end
if ~isa(obj1,'TSeries') || isempty(obj1.name), s1 = 'untitled';
else, s1 = obj1.name;
end
Ts.name = sprintf(['(%s' operationSymbol '%s)'],s,s1);
end
Ts.userData = [];
end
end
function Ts = uplus(obj)
% UPLUS The positive of the TS.
%
% TS.UPLUS(TS)
% +TS
Ts = obj;
end
function Ts = minus(obj,obj1)
% MINUS Subtraction of TS with TS/scalars.
%
% TS.MINUS(TS)
% TS - TS
%
% TS.MINUS(constant)
% TS - constant
% - Constant can be also an object of the same size
% as each data sample.
%
% Examples:
% TS3 = TS1 - TS2;
% TS2 = TS1 - 0.1;
% TS2 = TS1 - [1 2 4]; % if TS1,TS2 are vector time series
Ts = plus(obj,(-1)*obj1);
end
function Ts = uminus(obj)
% UMINUS The negative of the TS.
%
% TS.UMINUS(TS)
% -TS
Ts = obj;
Ts.data = - obj.data;
end
function Ts = dot(obj,obj1)
%DOT Scalar product
%
% TsOut = DOT(TsA,TsB)
% TsOut = DOT(TsA,VECTOR), VECTOR is a constant vector
if isa(obj1,'TSeries') && ~isa(obj,'TSeries')
obj1Tmp = obj1; obj1 = obj; obj = obj1Tmp;
end
if isa(obj1,'TSeries')
if obj.tensorOrder~=1 || obj1.tensorOrder~=1
error('Only vectors are supported');
elseif obj.time~=obj1.time
error('Input TS objects have different timelines, use resample()')
end
Ts = obj;
scalar_product()
else % constant vector, not TS
if ~isnumeric(obj1) || ~isvector(obj1)
error('Second argument must be a vector or TS')
end
Ts = obj;
switch obj.basis
case 'xy'
if length(obj1) ~= 2
error('Second argument must be a 2D vector')
end
Ts.data_ = obj.data(:,1).*obj1(1) + obj.data(:,2)*obj1(2);
case 'xyz'
if length(obj1) ~= 3
error('Second argument must be a 3D vector')
end
Ts.data_ = obj.data(:,1).*obj1(1) + obj.data(:,2)*obj1(2) + ...
+ obj.data(:,3)*obj1(3);
otherwise
error('Only Cartesian basis supported, use transform()'); % should not be here
end
end
update_name_units()
Ts.tensorOrder_=0; Ts.tensorBasis_ = ''; Ts.representation{2} = [];
function scalar_product()
switch obj.basis
case {'xy','rp'}
d1 = obj.transform('xy').data;
d2 = obj1.transform('xy').data;
Ts.data_ = d1(:,1).*d2(:,1) + d1(:,2).*d2(:,2);
case {'xyz','rtp','rlp','rpz'}
d1 = obj.transform('xyz').data;
d2 = obj1.transform('xyz').data;
Ts.data_ = d1(:,1).*d2(:,1) + d1(:,2).*d2(:,2) + ...
d1(:,3).*d2(:,3);
otherwise
error('Unknown representation'); % should not be here
end
end
function update_name_units()
if ~isempty(obj.name) || (isa(obj1,'TSeries') && ~isempty(obj1.name))
if isempty(obj.name), s = 'untitled';
else, s = obj.name;
end
if ~isa(obj1,'TSeries') || isempty(obj1.name), s1 = 'untitled';
else, s1 = obj1.name;
end
Ts.name = sprintf('dot(%s,%s)',s,s1);
end
if isempty(obj.units), s = '';
else, s = obj.units;
end
if ~isa(obj1,'TSeries') || isempty(obj1.units), s1 = '';
else, s1 = obj1.units;
end
Ts.units = sprintf('%s %s',s,s1);
Ts.userData = [];
end
end
function Ts = times(obj,obj1)
%TIMES element-wise multiplication '.*'.
if ~isa(obj1,'TSeries') || ~isa(obj,'TSeries')
if (isnumeric(obj1) || isnumeric(obj))
Ts = obj*obj1; return
else
error('TSeries inputs are expected')
end
elseif obj.tensorOrder~=obj1.tensorOrder
error('Inputs must have the same tensor order');
elseif obj.tensorBasis_ ~= obj1.tensorBasis_
error('Inputs must have the same tensor basis, use transform()');
elseif obj.time~=obj1.time
error('Input TS objects have different timelines, use resample()')
end
Ts = obj;
Ts.data_ = obj.data.*obj1.data;
update_name_units()
function update_name_units()
if ~isempty(obj.name) || ~isempty(obj1.name)
if isempty(obj.name), s = 'untitled';
else, s = obj.name;
end
if isempty(obj1.name), s1 = 'untitled';
else, s1 = obj1.name;
end
Ts.name = sprintf('%s * %s',s,s1);
end
if isempty(obj.units), s = '';
else, s = obj.units;
end
if isempty(obj1.units), s1 = '';
else, s1 = obj1.units;
end
Ts.units = sprintf('%s %s',s,s1);
Ts.userData = [];
end
end
function Ts = mtimes(obj1,obj2)
%MTIMES Matrix and scalar multiplication '*'.
% take first the simple cases of multiplication with scalar number
if isa(obj1,'TSeries') && isnumeric(obj2) && isscalar(obj2)
Ts = obj1;
Ts.data = obj1.data * obj2;
return;
end
if isa(obj2,'TSeries') && isnumeric(obj1) && isscalar(obj1)
Ts = obj2;
Ts.data = obj2.data * obj1;
return;
end
% Check dimensions of input
if isa(obj1,'TSeries')
sizeData1 = obj1.datasize('dataonly');
nTimes1 = obj1.length;
data1 = obj1.data;
tmpData1 = permute(obj1.data,[2:ndims(obj1.data) 1]); % move time to last index
tmpData1 = reshape(tmpData1,[sizeData1 nTimes1]);
to1 = obj1.tensorOrder;
repres1 = obj1.representation;
name1 = obj1.name;
units1 = obj1.units;
else
sizeData1 = size(obj1);
data1 = obj1;%size(reshape(obj1,[1 sizeObj1]));
tmpData1 = data1;
if isscalar(data1)
to1 = 0;
elseif ndims(data1)>2 % assume it's a scalar matrix
to1 = 1;
elseif any(sizeData1==1)
to1 = 1;
elseif all(size(data1)>1)
to1 = 2;
elseif any(size(data1)>1)
to1 = 1;
end
if to1 == 0
repres1 = {};
elseif to1 == 1
repres1 = {'x','y','z'};
elseif to1 == 2
repres1 = {{'x','y','z'};{'x','y','z'}};
end
name1 = '';
units1 = '';
end
if isa(obj2,'TSeries')
sizeData2 = obj2.datasize('dataonly');
nTimes2 = obj2.length;
data2 = obj2.data;
tmpData2 = permute(obj2.data,[2:ndims(obj2.data) 1]); % move time to last index
tmpData2 = reshape(tmpData2,[sizeData2 nTimes2]);
to2 = obj2.tensorOrder;
repres2 = obj2.representation;