attachment:Sampling.m of SignalAnalysisMatlabSchedule - Methods
location: attachment:Sampling.m of SignalAnalysisMatlabSchedule

Attachment 'Sampling.m'

Download

   1 %% Create example data
   2 x = 0:0.01:8*pi; % abscissa values
   3 y = x'/5 + sin(10*x)';
   4 plot(x,y);
   5 whos
   6 
   7 % Downsample by a factor 10
   8 x2 = x(1:10:end);
   9 y2 = x2'/5 + sin(10*x2)';
  10 figure;
  11 plot(x2,y2);
  12 whos
  13 
  14 % Downsample by a factor 100
  15 x3 = x(1:100:end);
  16 y3 = x3'/5 + sin(10*x3)';
  17 figure;
  18 plot(x3,y3);
  19 whos
  20 
  21 % Plot for PPT
  22 % figure;
  23 % plot(x,y, 'r');
  24 % hold on;
  25 % ph = plot(x3,y3,'xb');
  26 % set(ph, 'MarkerSize', 10);
  27 % hold on;
  28 % ph = plot(x3,-1,'xk');
  29 % set(ph, 'MarkerSize', 10);
  30 % hold on;
  31 % plot(x3,y3, 'b');
  32 
  33 %% Missing data points, interpolation
  34 % create faulty data
  35 y4 = y;
  36 y4(1000:1020) = 0;
  37 figure;
  38 plot(x,y4);
  39 
  40 % quick fix: replace by average of edges
  41 y4(1000:1020) = mean( y4([999, 1021]) );
  42 figure;
  43 plot(x,y4);
  44 
  45 % linear interpolation
  46 y4(1000:1020) = y4(999) + [1:21]*(y4(1021)-y4(999))/20;
  47 figure;
  48 plot(x,y4);
  49 
  50 % create "more faulty" data
  51 y4(1000:1100) = 0;
  52 figure;
  53 plot(x,y4);
  54 
  55 % linear interpolation
  56 y4(1000:1100) = y4(999) + [1:101]*(y4(1101)-y4(999))/100;
  57 figure;
  58 plot(x,y4);
  59 
  60 %% Signal-to-noise (SNR)
  61 clear all;  % let's start afresh
  62 close all;
  63 
  64 % Make a signal
  65 x = 0:0.01:4*pi; % abscissa values
  66 y1 = sin( x );
  67 y1(1:end/2) = 0; % include "baseline"
  68 plot(x,y1);
  69 
  70 % Make a slightly different signal
  71 y2 = 1.1 * y1;
  72 hold on;
  73 plot(x,y2, 'r')
  74 title('y1 and y2');
  75 
  76 % Add some noise
  77 lx = length(x);
  78 y1 = y1 + 0.05*randn(1,lx);
  79 y2 = y2 + 0.05*randn(1,lx);
  80 figure;
  81 plot(x,y1);
  82 hold on;
  83 plot(x,y2, 'r');
  84 title('y1 and y2 with noise');
  85 
  86 % compute SNR (power)
  87 vary1 = var(y1(1:end/2));
  88 vary2 = var(y2(1:end/2));
  89 figure;
  90 plot(x,y1.^2/vary1);
  91 hold on;
  92 plot(x,y2.^2/vary2, 'r'); % "zoom" into baseline to check it fluctuates around 1
  93 title('SNR, power');
  94 % Note: the power "rectifies" the signal, i.e. it's all positive
  95 
  96 % compute SNR (standard deviation)
  97 stdy1 = std(y1(1:end/2));
  98 stdy2 = std(y2(1:end/2));
  99 figure;
 100 plot(x,y1/stdy1);
 101 hold on;
 102 plot(x,y2/stdy2, 'r'); % "zoom" into baseline to check it fluctuates around 1
 103 title('SNR, std');
 104 
 105 % Root-mean-square (RMS)
 106 rmsy1 = sqrt( mean ( y1(1:end/2).^2 ) );
 107 rmsy2 = sqrt( mean ( y2(1:end/2).^2 ) );
 108 figure;
 109 plot(x,y1/rmsy1);
 110 hold on;
 111 plot(x,y2/rmsy2, 'r'); % "zoom" into baseline to check it fluctuates around 1
 112 title('SNR, RMS');
 113 
 114 
 115 %% Error propagation
 116 % subtraction
 117 diffy = y1-y2;
 118 figure;
 119 plot(x, diffy);
 120 title('Diff');
 121 
 122 var( y1(1:600) )
 123 var( y2(1:600) )
 124 var( diffy(1:600) )
 125 
 126 addy = y1+y2;
 127 var( addy(1:600) )
 128 
 129 % Error propagation can be disastrous
 130 figure;
 131 y1ori = sin( x );
 132 plot(x, y1ori); % plot sine curve
 133 
 134 hold on;
 135 ydiff = diff(y1ori);
 136 plot(x(2:end), 100*ydiff, 'r'); % plot "clean" derivate of sine curve
 137 title('Diff2')
 138 
 139 plot(x(2:end), 100*diff(y1), 'g'); % plot noisy derivative of sine curve
 140 
 141 %% Smoothing (get previous figure 'Diff' back)
 142 % command "smooth" is easy but not necessarily the best way for data filtering
 143 figure;
 144 sy = smooth(diffy, 10);
 145 plot(x, sy, 'r');
 146 title('smooth')
 147 
 148 sy = smooth(diffy, 100);    % smooth more
 149 hold on;
 150 plot(x, sy, 'k');
 151 
 152 % What could possibly go wrong?
 153 
 154 % Edge effects of filtering:
 155 figure;
 156 plot(x(2:end), diff(sy));
 157 
 158 % Oversmoothing:
 159 sy = smooth(diffy, 1000);   % smooth too much
 160 plot(x, sy, 'g');
 161 
 162 
 163 
 164 
 165 
 166 
 167 %% The END 

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.