smooth filtering shifts my original signal? - matlab

Here is my code:
sigma = 10;
sz = 20;
x = linspace(-sz / 2, sz / 2-1, sz);
gf = exp(-x .^ 2 / (2 * sigma ^ 2));
gf = gf / sum (gf); % normalize
f_filter = cconv(gf,f,length(f));
Basically I am Gaussian filtering original signal f. However, when I look at the filtered signal f_filter, there is a shift comparing the original signal f (See attached figure). I am not sure why this is happening. I would like to only smooth but not shift the orginal signal. Please help. Thanks.
my original signal f is here:
-0.0311
-0.0462
-0.0498
-0.0640
-0.0511
-0.0522
-0.0566
-0.0524
-0.0478
-0.0482
-0.0516
-0.0435
-0.0417
-0.0410
-0.0278
-0.0079
-0.0087
-0.0029
0.0105
0.0042
0.0046
0.0107
0.0119
0.0177
0.0077
0.0138
0.0114
0.0103
0.0089
0.0122
0.0122
0.0118
0.0041
0.0047
0.0062
0.0055
0.0033
0.0096
0.0062
-0.0013
0.0029
0.0112
0.0069
0.0160
0.0127
0.0131
0.0039
0.0116
0.0078
0.0018
0.0023
0.0133
0.0140
0.0135
0.0098
0.0100
0.0133
0.0131
0.0086
0.0114
0.0131
0.0175
0.0137
0.0157
0.0040
0.0136
0.0009
0.0049
0.0157
0.0104
0.0038
0.0039
0.0029
0.0126
0.0044
0.0055
0.0040
0.0091
-0.0023
0.0107
0.0151
0.0115
0.0135
0.0160
0.0071
0.0098
0.0094
0.0072
0.0079
0.0055
0.0155
0.0107
0.0108
0.0085
0.0099
0.0055
0.0078
0.0027
0.0121
0.0077
0.0062
0.0021
-0.0019
-0.0003
-0.0022
0.0059
0.0099
0.0114
0.0069
0.0038
0.0020
-0.0031
0.0024
-0.0025
-0.0004
0.0041
0.0059
0.0018
0.0033
0.0130
0.0131
0.0076
0.0084
0.0029
0.0086
0.0078
0.0054
0.0121
0.0101
0.0132
0.0115
0.0074
0.0070
0.0088
0.0017
-0.0003
-0.0060
0.0078
0.0100
0.0044
0.0017
0.0027
0.0062
0.0029
-0.0035
0.0032
0.0060
-0.0035
0.0081
0.0027
0.0043
0.0013
0.0049
0.0119
0.0273
0.0363
0.0435
0.0432
0.0357
0.0424
0.0318
0.0341
0.0354
0.0325
0.0263
0.0320
0.0312
0.0345
0.0407
0.0378
0.0376
0.0334
0.0381
0.0428
0.0375
0.0431
0.0403
0.0395
0.0308
0.0150
0.0006
0.0054
0.0002
0.0090
0.0075
0.0051
0.0067
0.0062
0.0108
0.0059
0.0095
0.0065
0.0087
0.0056
0.0136
0.0057
0.0079
0.0107
0.0106
0.0041
0.0032
0.0106
0.0091
0.0082
0.0025
0.0124
0.0035
0.0034
0.0097
0.0034
0.0050
0.0119
0.0087
0.0081
0.0118
0.0088
0.0050
0.0050
0.0057
0.0118
0.0122
0.0207
0.0112
0.0125
0.0083
0.0125
0.0140
0.0147
0.0237
0.0206
0.0141
0.0164
0.0189
0.0189
0.0136
0.0183
0.0195
0.0209
0.0154
0.0211
0.0254
0.0163
0.0249
0.0236
0.0262
0.0278
0.0285
0.0275
0.0212
0.0277
0.0211
0.0248
0.0289
0.0240
0.0266
0.0479
0.1744
0.4070
0.6818
0.8811
0.9859
0.9347
0.8441
0.7625
0.6396
0.4724
0.3639
0.3406
0.3406
0.3363
0.3318
0.3251
0.3287
0.3135
0.3122
0.3058
0.3103
0.3012
0.2974
0.2995
0.2941
0.2981
0.2968
0.2958
0.2938
0.2929
0.2926
0.2942
0.2982
0.2898
0.2940
0.2927
0.2950
0.2899
0.2979
0.2915
0.2961
0.2921
0.2931
0.2989
0.2941
0.2977
0.3041
0.3042
0.3086
0.3048
0.3069
0.3055
0.3123
0.3138
0.3128
0.3115
0.3092
0.3174
0.3152
0.3106
0.3080
0.3166
0.3109
0.3103
0.3135
0.3101
0.3133
0.3147
0.3044
0.2980
0.2972
0.3013
0.2980
0.3069
0.3932
0.6593
0.8921
1.1071
1.2763
1.3947
1.5076
1.6278
1.7452
1.7993
1.8287
1.8470
1.8957
1.9408
1.9791
2.0272
2.0686
2.0974
2.1335
2.1790
2.2134
2.2545
2.2903
2.3163
2.3585
2.3739
2.4126
2.4503
2.4787
2.5198
2.5447
2.5950
2.6228
2.6410
2.6812
2.7123
2.7557
2.8584
3.2480
3.5315
3.6808
3.7632
3.7471
3.7283
3.6692
3.6718
3.7756
3.9672
4.0376
3.9092
3.7276
3.6586
3.5948
3.6392
3.5671
3.6003
3.6194
3.6350
3.6624
3.6855
3.6958
3.9105
4.3880
5.1342
5.6176
6.3206
7.0392
7.3767
7.5715
7.6516
7.6469
7.5871
7.4591
7.6004
7.5532
7.3601
7.1487
5.9728
4.8974
4.5850
4.4268
4.3352
4.2887
4.3376
4.3182
4.2909
4.2777
4.2548
4.2677
4.2511
4.2817
4.3847
4.4418
4.4696
4.4932
4.4998
4.5151
4.5096
4.5278
4.5139
4.5020
4.4561
4.4067
4.3841
4.3638
4.3750
4.4366
4.5258
4.6565
4.6485
4.5836
4.5183
4.4583
4.3747
4.3509
4.2938
4.2823
4.2844
4.3135
4.3262
4.3255
4.2568
4.2011
4.1832
4.2278
4.2445
4.2409
4.2784
4.2917
4.3035
4.3015
4.3209
4.3204
4.3356
4.3287
4.3260
4.3483
4.3710
4.3798
4.3802
4.3805
4.5162
4.6906
5.0826
5.6588
6.0137
6.2436
6.5361
7.0790
7.6106
7.6410
7.4120
7.4535
7.2476
7.2596
7.1012
7.0986
6.9395
6.5633
5.8438
4.9434
4.6750
4.4320
4.3063
4.2096
4.0193
3.9698
4.0055
4.0218
4.0426
4.0688
4.0650
3.9793
3.9787
3.9766
3.9981
4.0405
4.0165
4.0290
4.0923
4.0897
4.0615
4.0258
4.0008
4.0274
4.0553
4.0646
4.0442
4.0477
3.9986
4.0354
4.0718
4.0563
4.0189
3.8631
3.8144
3.7736
3.8055
3.9730
4.0299
4.0148
3.8265
3.4675
3.3020
3.2474
3.2338
3.1986
3.1680
3.1289
3.0944
3.0523
3.0094
2.9510
2.9246
2.9057
2.8805
2.8545
2.8245
2.7690
2.7236
2.6833
2.6443
2.5969
2.5415
2.4684
2.4214
2.3699
2.3293
2.2513
2.1963
2.1285
2.0700
2.0209
1.9575
1.8658
1.6996
1.5120
1.4020
1.3087
1.2166
1.1441
1.0774
1.0226
0.9809
0.9448
0.8526
0.6915
0.4491
0.2842
0.2582
0.2570
0.2568
0.2609
0.2632
0.2581
0.2552
0.2539
0.2527
0.2578
0.2672
0.2701
0.2655
0.2658
0.2688
0.2761
0.2767
0.2738
0.2774
0.2801
0.2817
0.2803
0.2830
0.2828
0.2876
0.2952
0.2985
0.3016
0.3092
0.3130
0.3153
0.3182
0.3304
0.3471
0.3416
0.3476
0.3497
0.3453
0.3398
0.3448
0.3563
0.3511
0.3502
0.3481
0.3519
0.3573
0.3544
0.3512
0.3489
0.3499
0.3470
0.3533
0.3409
0.3556
0.3474
0.3435
0.3460
0.3519
0.3447
0.3395
0.3488
0.3473
0.3453
0.3433
0.3484
0.3526
0.3494
0.3607
0.3694
0.4126
0.4604
0.5004
0.5163
0.5328
0.5432
0.5506
0.5485
0.5605
0.5586
0.5622
0.5727
0.5804
0.5797
0.5666
0.5700
0.5696
0.5722
0.5715
0.5656
0.5572
0.5264
0.5156
0.5473
0.6286
0.7503
0.8715
0.8825
0.7507
0.5421
0.2869
0.1091
0.0423
0.0326
0.0343
0.0256
0.0231
0.0281
0.0298
0.0229
0.0283
0.0279
0.0270
0.0300
0.0245
0.0360
0.0280
0.0270
0.0232
0.0276
0.0270
0.0237
0.0197
0.0193
0.0172
0.0140
0.0093
0.0244
0.0226
0.0192
0.0145
0.0124
0.0167
0.0182
0.0111
0.0147
0.0081
0.0151
0.0130
0.0113
0.0131
0.0067
0.0028
0.0064
0.0069
0.0082
0.0075
0.0098
-0.0008
0.0037
0.0019
0.0060
0.0057
0.0033
0.0079
0.0122
0.0091
0.0067
-0.0038
0.0033
0.0013
0.0011
0.0034
0.0051
0.0009
-0.0001
-0.0005
0.0098
-0.0003
0.0067
0.0038
0.0106
0.0000
0.0126
0.0134
0.0090
0.0116
0.0083
0.0101
0.0152
0.0010
0.0068
0.0008
0.0053
0.0090
0.0087
0.0085
0.0054
0.0089
0.0077
0.0064
0.0046
0.0058
0.0025
0.0132
0.0088
0.0043
0.0052
0.0087
0.0122
0.0023
0.0066
0.0093
0.0042
0.0042
0.0138
0.0051
-0.0055
-0.0002
0.0048
0.0063
0.0076
0.0016
-0.0005
0.0086
0.0043
-0.0016
0.0100
0.0097
0.0042
0.0092
0.0051
0.0029
0.0044
0.0033
0.0073
0.0093
0.0077
0.0093
0.0021
0.0026
0.0093
0.0068
0.0039
0.0068
0.0041
0.0053
0.0037
0.0075
0.0016
0.0000
-0.0005
0.0073
0.0076
0.0049
0.0046
0.0087
0.0106
0.0072
0.0085
0.0036
0.0044
0.0043
0.0201
0.0076
0.0075
0.0134
0.0050
0.0071
0.0032
0.0055
0.0085
0.0046
0.0023
-0.0020
0.0027
0.0060
0.0066
0.0067
0.0014
0.0166
0.0067
0.0024
0.0072
0.0062
0.0081
0.0035
0.0077
0.0101
0.0045
0.0034
0.0144
0.0078
0.0065
0.0093
0.0181
0.0028
0.0050
0.0034
0.0063
0.0150
0.0035
0.0022
0.0079
0.0034
0.0110
0.0075
0.0058
0.0085
0.0152
0.0089
0.0060
0.0017
0.0041
0.0091
0.0072
-0.0109
0.0036
0.0063
0.0080
0.0037
0.0086
0.0097
0.0088
0.0016
0.0057
0.0059
0.0139
0.0061
0.0009
0.0059
0.0126
0.0117
0.0003
0.0060
0.0075
0.0073
0.0080
0.0154
0.0136
0.0121
0.0179
0.0150
0.0125

Instead of doing
f_filter = cconv(gf,f,length(f));
this does the trick:
f_filter = conv(gf,f);
f_filter = f_filter(sz/2+1:end-sz/2+1);

As suggested by #AnderBiguri you can use the option 'same' in your convolution fonction to preserve the original size of your array.
But if you apply a convolution with your normalized gaussian filter gf you will obtain a border effect.
To avoid the border effect you can apply the following tricks:
gf = exp(-x .^ 2 / (2 * sigma ^ 2)); %do not normalize gf now
f_filter = conv(f,gf,'same')./conv(ones(length(f),1),gf,'same') %normalization taking into account the lenght of the convolution
For example I've just transformed f into f = f+3
If we do not take into account the border effect we will obtain:

Related

continuous wavelet transfrom of a signal

I want to carry out continuous wavelet transform of a signal. I tried to write a script but the script give output of very low resolution scalogram. So I need a high resolution output by changing some scales. The signal data and the script is attached below.
I tried the script
from scipy import signal
import numpy as np
import matplotlib.pyplot as plt
import obspy
w=np.loadtxt('signal')
t = np.arange(0,len(w))
fmin = 1 # Hz
fmax = 50 # Hz
df = 1./(t[-1]-t[0])
print(df)
fmin_samples = int(fmin/df)
fmax_samples = int(fmax/df)
extent = np.arange(1,10)
scalogram = signal.cwt(w, signal.morlet,extent)
fig, ax = plt.subplots(2, 1, sharex=True)
ax[0].plot(t, w)
ax[0].set(ylabel='amplitude')
ax[1].imshow(np.abs(scalogram), origin='lower')
ax[1].axis('tight')
ax[1].set(xlabel='time (s)', ylabel='frequency (Hz)')
plt.show()
signal data
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
-0.0000
-0.0000
-0.0000
-0.0000
-0.0000
-0.0000
-0.0000
-0.0000
-0.0000
-0.0000
-0.0000
-0.0000
-0.0000
-0.0000
-0.0001
-0.0001
-0.0001
-0.0001
-0.0001
-0.0001
-0.0001
-0.0001
-0.0001
-0.0001
-0.0001
-0.0001
-0.0001
-0.0001
-0.0001
-0.0001
-0.0002
-0.0002
-0.0002
-0.0002
-0.0002
-0.0002
-0.0002
-0.0002
-0.0002
-0.0003
-0.0003
-0.0003
-0.0003
-0.0003
-0.0004
-0.0004
-0.0004
-0.0004
-0.0005
-0.0005
-0.0006
-0.0006
-0.0007
-0.0007
-0.0008
-0.0009
-0.0009
-0.0010
-0.0011
-0.0013
-0.0014
-0.0015
-0.0017
-0.0019
-0.0022
-0.0025
-0.0028
-0.0032
-0.0037
-0.0043
-0.0051
-0.0060
-0.0072
-0.0087
-0.0108
-0.0136
-0.0178
-0.0241
-0.0338
-0.0493
-0.0724
-0.1014
-0.1231
-0.1059
-0.0038
0.2143
0.5147
0.7698
0.8049
0.5211
-0.0000
-0.5212
-0.8050
-0.7698
-0.5148
-0.2144
0.0038
0.1059
0.1231
0.1013
0.0725
0.0492
0.0340
0.0240
0.0174
0.0136
0.0106
0.0086
0.0070
0.0059
0.0049
0.0042
0.0036
0.0031
0.0026
0.0023
0.0020
0.0018
0.0015
0.0014
0.0012
0.0010
0.0009
0.0008
0.0007
0.0006
0.0005
0.0004
0.0003
0.0003
0.0002
0.0001
0.0000
-0.0000
-0.0001
-0.0002
-0.0003
-0.0004
-0.0005
-0.0006
-0.0007
-0.0008
-0.0010
-0.0011
-0.0013
-0.0015
-0.0018
-0.0020
-0.0024
-0.0028
-0.0034
-0.0040
-0.0049
-0.0060
-0.0074
-0.0094
-0.0123
-0.0167
-0.0235
-0.0344
-0.0506
-0.0709
-0.0861
-0.0740
-0.0026
0.1501
0.3604
0.5389
0.5635
0.3649
0.0001
-0.3648
-0.5634
-0.5388
-0.3603
-0.1500
0.0027
0.0742
0.0862
0.0710
0.0508
0.0345
0.0239
0.0169
0.0123
0.0096
0.0075
0.0062
0.0050
0.0042
0.0035
0.0030
0.0026
0.0023
0.0020
0.0017
0.0015
0.0014
0.0012
0.0011
0.0010
0.0009
0.0008
0.0007
0.0007
0.0006
0.0006
0.0005
0.0005
0.0004
0.0004
0.0004
0.0003
0.0003
0.0003
0.0003
0.0003
0.0002
0.0002
0.0002
0.0002
0.0002
0.0002
0.0002
0.0002
0.0001
0.0001
0.0001
0.0001
0.0001
0.0001
0.0001
0.0001
0.0001
0.0001
0.0001
0.0001
0.0001
0.0001
0.0001
0.0001
0.0001
0.0000
0.0000
0.0000
0.0000
0.0000
Using a library named PyWavelets (documentation here) you can do it in a straightforward way
import pywt
import pylab
[phi, psi, x] = pywt.Wavelet('db2').wavefun(level=4)
pylab.plot(x, psi)
pylab.show()

Gaussian filter kernel different from Matlab Gaussian filter kernel

I am creating a Gaussian filter in Matlab. I have created the following code for creating the kernel.
function kernel = gaussian_filter(sigma)
kernel_width = 3 * sigma - 1;
[x, y] = meshgrid(-kernel_width/2:kernel_width/2, -kernel_width/2:kernel_width/2);
normalized_constant = 1/(2 * pi * sigma * sigma);
kernel = normalized_constant * exp(-(x.^2 + y.^2)/ (2 * sigma * sigma));
K = mat2gray(kernel);
imshow(K);
title('Gaussian Kernel');
end
And my output is:
gaussian_filter(3)
ans =
Columns 1 through 7
0.0030 0.0044 0.0058 0.0069 0.0073 0.0069 0.0058
0.0044 0.0065 0.0086 0.0101 0.0107 0.0101 0.0086
0.0058 0.0086 0.0113 0.0134 0.0142 0.0134 0.0113
0.0069 0.0101 0.0134 0.0158 0.0167 0.0158 0.0134
0.0073 0.0107 0.0142 0.0167 0.0177 0.0167 0.0142
0.0069 0.0101 0.0134 0.0158 0.0167 0.0158 0.0134
0.0058 0.0086 0.0113 0.0134 0.0142 0.0134 0.0113
0.0044 0.0065 0.0086 0.0101 0.0107 0.0101 0.0086
0.0030 0.0044 0.0058 0.0069 0.0073 0.0069 0.0058
Columns 8 through 9
0.0044 0.0030
0.0065 0.0044
0.0086 0.0058
0.0101 0.0069
0.0107 0.0073
0.0101 0.0069
0.0086 0.0058
0.0065 0.0044
0.0044 0.0030
But when I run the Matlab Gaussian filter, the result is slightly off my output.
h = fspecial('gaussian', 9, 3)
h =
Columns 1 through 7
0.0040 0.0059 0.0077 0.0091 0.0096 0.0091 0.0077
0.0059 0.0086 0.0114 0.0135 0.0142 0.0135 0.0114
0.0077 0.0114 0.0150 0.0178 0.0188 0.0178 0.0150
0.0091 0.0135 0.0178 0.0210 0.0222 0.0210 0.0178
0.0096 0.0142 0.0188 0.0222 0.0235 0.0222 0.0188
0.0091 0.0135 0.0178 0.0210 0.0222 0.0210 0.0178
0.0077 0.0114 0.0150 0.0178 0.0188 0.0178 0.0150
0.0059 0.0086 0.0114 0.0135 0.0142 0.0135 0.0114
0.0040 0.0059 0.0077 0.0091 0.0096 0.0091 0.0077
Columns 8 through 9
0.0059 0.0040
0.0086 0.0059
0.0114 0.0077
0.0135 0.0091
0.0142 0.0096
0.0135 0.0091
0.0114 0.0077
0.0086 0.0059
0.0059 0.0040
I am not missing any steps in the algorithm. I am trying to figure out why our results don't match.
I forget to normalize kernel:
kernel = kernel/(sum(kernel(:)));
https://www.mathworks.com/help/images/ref/fspecial.html

Matlab data sampler that weights the outputs to binary (0,1)

a sample probability matrix:
ans =
0.1444 0.0456 0.0138 0.0126 0.0116 0.0107 0.0052
0.1444 0.0456 0.0138 0.0126 0.0116 0.0107 0.0052
0.1222 0.0386 0.0116 0.0106 0.0098 0.0091 0.0044
0.1444 0.0456 0.0138 0.0126 0.0116 0.0107 0.0052
0.1222 0.0386 0.0116 0.0106 0.0098 0.0091 0.0044
0.1889 0.0596 0.0180 0.0164 0.0151 0.0140 0.0067
0.1333 0.0421 0.0127 0.0116 0.0107 0.0099 0.0048
I have used dataSample and randSample to sample 128 time from my data which has A=(7,7) size in matlab:
datasample(A,128)
ans =
0.1333 0.0421 0.0127 0.0116 0.0107 0.0099 0.0048
0.1222 0.0386 0.0116 0.0106 0.0098 0.0091 0.0044
0.1889 0.0596 0.0180 0.0164 0.0151 0.0140 0.0067
0.1889 0.0596 0.0180 0.0164 0.0151 0.0140 0.0067
0.1333 0.0421 0.0127 0.0116 0.0107 0.0099 0.0048
0.1444 0.0456 0.0138 0.0126 0.0116 0.0107 0.0052
0.1222 0.0386 0.0116 0.0106 0.0098 0.0091 0.0044
...
However, I am interested in having those 128 sample of 7 (128,7) in binary format with two discrete values of 0 and 1:
[1 1 1 0 1 0 1]
I can write a loop and round-down/up those values to 0 and 1 with certain thresholds (i.e. 0.5), but that for sure will be noisy. Is there a function that can output the sampling in binary (0,1) in Matlab ?

MvNormal Error with Symmetric & Positive Semi-Definite Matrix

The summary of my problem is that I am trying to replicate the Matlab function:
mvnrnd(mu', sigma, 200)
into Julia using:
rand( MvNormal(mu, sigma), 200)'
and the result is a 200 x 7 matrix, essentially generating 200 random return time series data.
Matlab works, Julia doesn't.
My input matrices are:
mu = [0.15; 0.03; 0.06; 0.04; 0.1; 0.02; 0.12]
sigma = [0.0035 -0.0038 0.0020 0.0017 -0.0006 -0.0028 0.0009;
-0.0038 0.0046 -0.0011 0.0001 0.0003 0.0054 -0.0024;
0.0020 -0.0011 0.0041 0.0068 -0.0004 0.0047 -0.0036;
0.0017 0.0001 0.0068 0.0125 0.0002 0.0109 -0.0078;
-0.0006 0.0003 -0.0004 0.0002 0.0025 -0.0004 -0.0007;
-0.0028 0.0054 0.0047 0.0109 -0.0004 0.0159 -0.0093;
0.0009 -0.0024 -0.0036 -0.0078 -0.0007 -0.0093 0.0061]
Using Distributions.jl, running the line:
MvNormal(sigma)
Produces the error:
ERROR: LoadError: Base.LinAlg.PosDefException(4)
The matrix sigma is symmetrical but only positive semi-definite:
issym(sigma) #symmetrical
> true
isposdef(sigma) #positive definite
> false
using LinearOperators
check_positive_definite(sigma) #check for positive (semi-)definite
> true
Matlab produces the same results for these tests however Matlab is able to generate the 200x7 random return sample matrix.
Could someone advise as to what I could do to get it working in Julia? Or where the issue lies?
Thanks.
The issue is that the covariance matrix is indefinite. See
julia> eigvals(sigma)
7-element Array{Float64,1}:
-3.52259e-5
-2.42008e-5
2.35508e-7
7.08269e-5
0.00290538
0.0118957
0.0343873
so it is not a covariance matrix. This might have happened because of rounding so if you have access to unrounded data you can try that instead. I just tried and I also got an error in Matlab. However, in contrast to Julia, Matlab does allow the matrix to be positive semidefinite.
A way to make this work is to add a diagonal matrix to the original matrix and then input that to MvNormal. I.e.
julia> MvNormal(randn(7), sigma - minimum(eigvals(Symmetric(sigma)))*I)
Distributions.MvNormal{PDMats.PDMat{Float64,Array{Float64,2}},Array{Float64,1}}(
dim: 7
μ: [0.889004,-0.768551,1.78569,0.130445,0.589029,0.529418,-0.258474]
Σ: 7x7 Array{Float64,2}:
0.00353523 -0.0038 0.002 0.0017 -0.0006 -0.0028 0.0009
-0.0038 0.00463523 -0.0011 0.0001 0.0003 0.0054 -0.0024
0.002 -0.0011 0.00413523 0.0068 -0.0004 0.0047 -0.0036
0.0017 0.0001 0.0068 0.0125352 0.0002 0.0109 -0.0078
-0.0006 0.0003 -0.0004 0.0002 0.00253523 -0.0004 -0.0007
-0.0028 0.0054 0.0047 0.0109 -0.0004 0.0159352 -0.0093
0.0009 -0.0024 -0.0036 -0.0078 -0.0007 -0.0093 0.00613523
)
The "covariance" matrix is of course not the same anymore, but it is very close.

Finding local minima and local maxima [duplicate]

This question already has answers here:
Find local maximum value in the vector
(4 answers)
Closed 8 years ago.
I would like to find local minima and local maxima of a given vector. Let's assume that the given vector is as follow:
speed =
0.0002
0.0008
0.0014
0.0027
0.0037
0.0047
0.0054
0.0053
0.0053
0.0058
0.0060
0.0063
0.0062
0.0065
0.0062
0.0061
0.0060
0.0057
0.0062
0.0057
0.0053
0.0050
0.0047
0.0065
0.0049
0.0048
0.0033
0.0033
0.0041
0.0049
0.0063
0.0075
0.0085
0.0105
0.0108
0.0109
0.0105
0.0105
0.0099
0.0098
0.0099
0.0099
0.0105
0.0103
0.0112
0.0108
0.0088
0.0079
0.0066
0.0055
0.0058
0.0049
0.0049
0.0055
0.0060
0.0051
0.0055
0.0060
0.0053
0.0047
0.0058
0.0050
0.0044
0.0033
0.0022
0.0008
0.0015
0.0010
0.0011
0.0024
0.0028
0.0024
0.0016
0.0009
0.0009
0.0009
0.0015
0.0015
0.0025
0.0031
0.0030
0.0042
0.0051
0.0060
0.0065
0.0054
0.0012
0.0043
0.0059
0.0070
0.0078
0.0076
0.0082
0.0087
0.0088
0.0095
0.0101
0.0100
0.0110
0.0103
0.0111
0.0120
0.0118
0.0116
0.0115
0.0121
0.0120
0.0145
0.0107
0.0119
0.0110
0.0116
0.0102
0.0086
0.0076
0.0071
0.0055
0.0066
0.0063
0.0077
0.0052
0.0059
0.0061
0.0036
0.0047
0.0053
0.0027
0.0020
0.0011
0.0041
0.0034
0.0034
0.0019
0.0022
0.0008
0.0001
0.0007
0.0009
0.0010
0.0010
0.0001
0.0007
0.0014
0.0016
0.0016
0.0013
0.0008
0.0008
0.0005
0.0004
0.0002
0.0001
0.0004
0.0005
0.0006
0.0005
0.0006
0.0006
0.0004
0.0002
0.0000
0.0001
0.0001
0.0002
0.0003
0.0004
0.0004
0.0005
0.0007
0.0008
0.0007
0.0006
0.0005
0.0006
0.0006
0.0004
0.0002
0.0003
0.0006
0.0005
0.0005
0.0010
0.0012
0.0014
0.0020
0.0028
0.0039
0.0044
0.0061
0.0074
0.0082
0.0091
0.0102
0.0108
0.0110
0.0117
0.0128
0.0133
0.0148
0.0153
0.0155
0.0150
0.0146
0.0137
0.0130
0.0113
0.0110
0.0107
0.0112
0.0114
0.0113
0.0104
0.0101
0.0095
0.0088
0.0083
0.0076
0.0057
0.0047
0.0043
0.0046
0.0053
0.0063
0.0078
0.0070
0.0062
0.0053
0.0051
0.0055
0.0048
0.0053
0.0052
0.0055
0.0065
0.0075
0.0078
0.0081
0.0067
0.0044
0.0061
0.0047
0.0032
0.0033
0.0028
0.0019
0.0007
0.0017
0.0016
0.0025
0.0034
0.0037
0.0044
0.0039
0.0037
0.0029
0.0030
0.0025
0.0022
0.0025
0.0027
0.0028
0.0031
0.0029
0.0025
0.0025
0.0025
0.0024
0.0022
0.0021
0.0019
0.0020
0.0020
0.0016
0.0016
0.0015
0.0013
0.0011
0.0011
0.0010
0.0009
0.0008
0.0006
0.0005
0.0004
0.0002
0.0000
0.0002
0.0003
0.0004
0.0006
0.0005
0.0004
0.0003
0.0004
0.0003
0.0003
0.0004
0.0006
0.0004
0.0004
when I polt this vector in Matlab using command plot(speed) then I have the following figure:
How could I find the maxima and minima's of the given vector? For example, in this above picture my aim is to find the three minimums/maximums that are shown in the picture.
I have lots of such a vectors that I want to write a code for all to find local minimas and maximas as well.
First of all you need to define what you count as extremum (maximum or minimum), i.e. which scale is considered appropriate, as your curve in reality has much more local maxima and minima than 3 or 4. Therefore looking for zero-crossings of the first derivative with diff will give you a lots of spurious micro-peaks. One option is to smooth it before. However, it might be easier to resort to a standard tool.
Try findpeaks from Signal Processing Toolbox.
There you can specify the scale with various parameters, such as 'MinPeakDistance', 'MinPeakHeight', 'Threshold' etc.