attachment:Olaf IntroGLM2 4Feb15.m of SignalAnalysisMatlabSchedule - Methods
location: attachment:Olaf IntroGLM2 4Feb15.m of SignalAnalysisMatlabSchedule

Attachment 'Olaf IntroGLM2 4Feb15.m'

Download

   1 %% basics of linear equations
   2 
   3 % examples of linear equations
   4 
   5 % invertible
   6 M1 = [1 0; -1 1]
   7 y = [1 2]'
   8 % solution
   9 inv(M1)*y
  10 
  11 % non-invertible
  12 M2 = [1 -1; -1 1]
  13 inv(M2)*y  % doesn't work
  14 
  15 % invertible but possibly instable
  16 M3 = [1 -1; -1 1.1]
  17 inv(M3)*y
  18 
  19 %% rank
  20 rank(M1)
  21 rank(M2)
  22 rank(M3)
  23 
  24 % What is the result of rank( zeros(1000) )?
  25 % Does the rank change when multiplying the matrix with a scalar?
  26 
  27 %% SVD for square matrix
  28 M4 = [-0.5 0.5; 1 1];
  29 [U,S] = svd( M4 )
  30 % condition number
  31 S(1,1)/S(2,2)
  32 
  33 % change the similarity of columns and see what happens
  34 M4 = [-0.1 0.1; 1 1];
  35 [U,S] = svd( M4 )
  36 % condition number
  37 S(1,1)/S(2,2)
  38 
  39 M4 = [-1 1; 1 1];
  40 [U,S] = svd( M4 )
  41 % condition number
  42 S(1,1)/S(2,2)
  43 
  44 % the vectors in U are orthonormal (orthogonal and unit length)
  45 U'*U
  46 
  47 %% SVD for rectangular matrix
  48 M = [ [2 2 0]', [0 0 1]' ]
  49 [U,S,V] = svd(M)
  50 
  51 % check whether decomposition works out
  52 U*S*V'
  53 
  54 %% Why "eigen"vectors?
  55 % when you stick eigenvectors into a covariance matrix...
  56 (M*M')*U
  57 
  58 % you get the vectors back, multiplied by square of eigenvalues
  59 U*S.^2
  60 
  61 % same for V
  62 (M'*M)*V
  63 S.^2*V
  64 
  65 %% Why is this useful - SVD as a filter
  66 % make some data
  67 x = [-2*pi:0.01:2*pi];
  68 data = [sin(x)' cos(x)'];
  69 
  70 plot(data)
  71 
  72 % replicate this matrix
  73 data = repmat(data, 1, 10);
  74 whos
  75 
  76 % add noise
  77 data = data + randn(size(data));
  78 plot(data)
  79 
  80 % 'economy size' svd
  81 [U,S,V] = svd( data, 0 );
  82 
  83 % plot eigenvalues
  84 sdiag = diag(S)
  85 figure
  86 plot( sdiag, 'x' )
  87 % if you want to plot variances
  88 svar = 100 * sdiag.^2 / sum( sdiag.^2 )
  89 plot(svar, 'x')
  90 
  91 % leave only first two SVD components
  92 sdiag2 = sdiag;
  93 sdiag2(3:end) = 0
  94 S2 = diag(sdiag2);
  95 
  96 % reassamble filtered data
  97 data2 = U*S2*V';
  98 figure
  99 plot(data2)
 100 
 101 %% Why is this useful - SVD to compute pseudoinverse
 102 
 103 % get M4 from above
 104 M4 = [-0.1 0.1; 1 1];
 105 [U,S,V] = svd( M4 )
 106 
 107 % invert the eigenvalues
 108 S(1,1) = 1/S(1,1)
 109 S(2,2) = 1/S(2,2)
 110 
 111 % compute the inverse
 112 myinv = V*S*U'
 113 
 114 % check whether it's correct
 115 inv(M4)
 116 
 117 % "dampen" the effect of small eigenvalues
 118 S(2,2) = 5
 119 
 120 % see how this affects the inverse
 121 myinv2 = V*S*U'
 122 
 123 % apply to some "data" vector y and note the difference
 124 y = [1 1]'
 125 
 126 myinv*y
 127 myinv2*y
 128 
 129 % the data that these solutions would produce
 130 M4*myinv*y      % exact
 131 M4*myinv2*y     % approximate
 132 
 133 
 134 %% Relationship PCA and SVD
 135 
 136 % The Matlab command princomp removes means from columns,
 137 % which differs from svd()
 138 % Therefore, let's start with a column-centred matrix
 139 
 140 X = [1 1 -2; 1 -2 1; -2 1 1]
 141 mean(X)
 142 % what's the rank of this matrix?
 143 
 144 % PCA
 145 [A,B,C] = princomp(X)
 146 
 147 % SVD
 148 [U,S,V] = svd(X)
 149 
 150 % note: a and v are the same, because for princomp rows are observations
 151 
 152 % s and c should contain the same values, but c is half of s-squared
 153 % reason: s-square is the "raw" variance, and princomp divideds by the
 154 % degrees of freedom (here: 2)

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2018-11-15 14:50:04, 2556.1 KB) [[attachment:ArcherBoyd+Guerit_vocoder_Nov18.zip]]
  • [get | view] (2019-11-21 17:21:33, 1661.8 KB) [[attachment:CBU_FunctionsVocoder_22Nov19.zip]]
  • [get | view] (2019-11-19 16:53:57, 4.6 KB) [[attachment:Code_Optimisation_20Nov19.zip]]
  • [get | view] (2016-03-09 10:59:16, 259.0 KB) [[attachment:DP_Optimisation_2016.zip]]
  • [get | view] (2015-01-20 17:22:38, 614.9 KB) [[attachment:EMEGdata.mat]]
  • [get | view] (2016-02-03 12:32:14, 26.8 KB) [[attachment:ExampleScripts.zip]]
  • [get | view] (2018-01-12 13:39:53, 3150.4 KB) [[attachment:Filtering&Oscillations_ForUpload_TallieJan18.pdf]]
  • [get | view] (2018-07-19 14:49:35, 2.5 KB) [[attachment:Functions and Calculus.m]]
  • [get | view] (2018-07-19 14:49:30, 433.8 KB) [[attachment:Functions and Calculus.pdf]]
  • [get | view] (2015-02-02 09:27:07, 522.0 KB) [[attachment:GLM1.pdf]]
  • [get | view] (2019-11-19 13:06:58, 790.4 KB) [[attachment:GLM1_19Nov19.pdf]]
  • [get | view] (2019-11-19 13:07:11, 2.5 KB) [[attachment:GLM1_19Nov19.zip]]
  • [get | view] (2018-01-12 13:00:28, 9.0 KB) [[attachment:GLM1_Jan2018.m]]
  • [get | view] (2018-01-12 13:02:14, 354.1 KB) [[attachment:GLM1_Jan2018.pdf]]
  • [get | view] (2018-11-13 11:34:38, 11.5 KB) [[attachment:GLM1_Nov2018.m]]
  • [get | view] (2018-11-13 11:34:31, 1417.1 KB) [[attachment:GLM1_Nov2018.pptx]]
  • [get | view] (2018-11-13 12:07:58, 11.5 KB) [[attachment:GLM1_Nov2018_ES18.m]]
  • [get | view] (2018-11-13 12:07:52, 271.1 KB) [[attachment:GLM1_Nov2018_ES18.pdf]]
  • [get | view] (2015-02-02 09:27:27, 2.1 KB) [[attachment:GLM1_script.m]]
  • [get | view] (2016-02-11 18:26:35, 560.4 KB) [[attachment:GLM_part1.pdf]]
  • [get | view] (2018-01-08 09:49:09, 8.5 KB) [[attachment:GLM_workshop.m]]
  • [get | view] (2015-02-18 12:51:09, 580.2 KB) [[attachment:IntroCalculus 18Feb15.pdf]]
  • [get | view] (2015-03-02 18:21:14, 4.3 KB) [[attachment:IntroFilteringOscillations_25Feb15.zip]]
  • [get | view] (2016-02-18 15:52:26, 237.9 KB) [[attachment:IntroGLM2_17Feb16.pdf]]
  • [get | view] (2015-02-04 11:58:10, 125.6 KB) [[attachment:IntroGLM2_4Feb15.pdf]]
  • [get | view] (2016-02-04 17:19:34, 445.3 KB) [[attachment:IntroMatrixAlgebra.pdf]]
  • [get | view] (2015-03-04 12:00:10, 1.3 KB) [[attachment:IntroOptimisation_4Mar15.zip]]
  • [get | view] (2018-01-09 12:44:37, 19.3 KB) [[attachment:Matlab_Files.zip]]
  • [get | view] (2015-01-20 17:29:01, 6.1 KB) [[attachment:MatrixAlgebra.m]]
  • [get | view] (2018-11-13 12:08:14, 917.4 KB) [[attachment:Matrix_Algebra_AT18.pdf]]
  • [get | view] (2015-03-02 18:19:54, 260.9 KB) [[attachment:Olaf FilteringOscillations 25Feb15.pdf]]
  • [get | view] (2015-02-18 12:51:00, 1.8 KB) [[attachment:Olaf IntroCalculus 18Feb15 Matlab.zip]]
  • [get | view] (2016-02-18 15:52:39, 2.9 KB) [[attachment:Olaf IntroGLM2 17Feb2016.m]]
  • [get | view] (2015-02-04 11:58:19, 2.9 KB) [[attachment:Olaf IntroGLM2 4Feb15.m]]
  • [get | view] (2015-03-04 12:00:04, 113.0 KB) [[attachment:Olaf Optimisation 4Mar15.pdf]]
  • [get | view] (2015-01-16 13:56:32, 3.2 KB) [[attachment:Sampling.m]]
  • [get | view] (2015-01-16 13:56:14, 754.0 KB) [[attachment:Sampling.pdf]]
  • [get | view] (2016-02-04 17:19:23, 1107.2 KB) [[attachment:SignalSamplingNoise.pdf]]
  • [get | view] (2018-11-13 12:08:19, 1479.3 KB) [[attachment:Signal_sampling_noise_AT18.pdf]]
  • [get | view] (2018-01-11 09:31:52, 259.2 KB) [[attachment:optimisation.zip]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.