continuous wavelet transfrom of a signal - scipy

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()

Related

peculiarity about creating a file using output redirection and opening a file with cat

I noticed that creating a file using output redirection involves neither creat() or open() system call. I thinks this is due to the stdin and stdout always exist and are always open. But how can I detect (i.e., using dtrace) file creation/read/write in those case?
Please see below turss outputs.
echo 888 >/var/tmp/testfile1
7570/1: 0.0022 0.0022 0.0000 sysinfo(SI_MACHINE, "i86pc", 257) = 6
7570/1: 0.0022 0.0000 0.0000 mmap(0x00000000, 32, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON, -1, 0) = 0xFEFF0000
7570/1: 0.0023 0.0001 0.0000 mmap(0x00000000, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0) = 0xFEFB0000
7570/1: 0.0024 0.0001 0.0000 mmap(0x00000000, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0) = 0xFEFA0000
7570/1: 0.0024 0.0000 0.0000 memcntl(0xFEFBE000, 13608, MC_ADVISE, MADV_WILLNEED, 0, 0) = 0
7570/1: 0.0025 0.0001 0.0000 mmap(0x00000000, 4096, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON, -1, 0) = 0xFEF90000
7570/1: 0.0025 0.0000 0.0000 memcntl(0x08050000, 1708, MC_ADVISE, MADV_WILLNEED, 0, 0) = 0
7570/1: 0.0026 0.0001 0.0000 resolvepath("/usr/lib/ld.so.1", "/lib/ld.so.1", 1023) = 12
7570/1: 0.0027 0.0001 0.0000 resolvepath("/usr/bin/echo", "/usr/bin/echo", 1023) = 13
7570/1: 0.0027 0.0000 0.0000 sysconfig(_CONFIG_PAGESIZE) = 4096
7570/1: 0.0028 0.0001 0.0000 stat64("/usr/bin/echo", 0x08045C10) = 0
7570/1: 0.0028 0.0000 0.0000 open("/var/ld/ld.config", O_RDONLY) Err#2 ENOENT
7570/1: 0.0029 0.0001 0.0000 stat64("/usr/lib/libc.so.1", 0x080454C0) = 0
7570/1: 0.0030 0.0001 0.0000 resolvepath("/usr/lib/libc.so.1", "/lib/libc.so.1", 1023) = 14
7570/1: 0.0030 0.0000 0.0000 open("/usr/lib/libc.so.1", O_RDONLY) = 3
7570/1: 0.0031 0.0001 0.0000 mmap(0x00010000, 32768, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_ALIGN, 3, 0) = 0xFEF80000
7570/1: 0.0031 0.0000 0.0000 mmap(0x00010000, 1155072, PROT_NONE, MAP_PRIVATE|MAP_NORESERVE|MAP_ANON|MAP_ALIGN, -1, 0) = 0xFEE60000
7570/1: 0.0032 0.0001 0.0000 mmap(0xFEE60000, 1110613, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_TEXT, 3, 0) = 0xFEE60000
7570/1: 0.0032 0.0000 0.0000 mmap(0xFEF70000, 30255, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_INITDATA, 3, 1114112) = 0xFEF70000
7570/1: 0.0033 0.0001 0.0000 mmap(0xFEF78000, 4200, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_ANON, -1, 0) = 0xFEF78000
7570/1: 0.0033 0.0000 0.0000 munmap(0xFEF80000, 32768) = 0
7570/1: 0.0034 0.0001 0.0000 close(3) = 0
7570/1: 0.0034 0.0000 0.0000 mmap(0x00000000, 12288, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON, -1, 0) = 0xFEF80000
7570/1: 0.0035 0.0001 0.0000 memcntl(0xFEE60000, 124760, MC_ADVISE, MADV_WILLNEED, 0, 0) = 0
7570/1: 0.0038 0.0003 0.0000 mmap(0x00010000, 24576, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON|MAP_ALIGN, -1, 0) = 0xFEE50000
7570/1: 0.0039 0.0001 0.0000 getcontext(0x08045A80)
7570/1: 0.0039 0.0000 0.0000 getrlimit(RLIMIT_STACK, 0x08045A78) = 0
7570/1: 0.0039 0.0000 0.0000 getpid() = 7570 [7569]
7570/1: 0.0040 0.0001 0.0000 lwp_private(0, 1, 0xFEE52A00) = 0x000001C3
7570/1: 0.0041 0.0001 0.0000 setustack(0xFEE52A60)
7570/1: 0.0041 0.0000 0.0000 sysi86(SI86FPSTART, 0xFEF78718, 0x0000133F, 0x00001F80) = 0x00000001
7570/1: 0.0042 0.0001 0.0000 ioctl(1, TCGETA, 0x08045E64) Err#25 ENOTTY
7570/1: 0.0043 0.0001 0.0000 fstat64(1, 0x08045E90) = 0
7570/1: 0.0042 0.0001 0.0000 ioctl(1, TCGETA, 0x08045E64) Err#25 ENOTTY
7570/1: 0.0043 0.0001 0.0000 fstat64(1, 0x08045E90) = 0
7570/1: 0.0043 0.0000 0.0000 brk(0x08061710) = 0
7570/1: 0.0044 0.0001 0.0000 brk(0x08083710) = 0
7570/1: 0.0044 0.0000 0.0000 fstat64(1, 0x08045DD0) = 0
7570/1: 0.0045 0.0001 0.0000 write(1, " 8 8 8\n", 4) = 4
7570/1: 0.0046 0.0001 0.0000 _exit(0)
Redirection is done by your shell, not the echo command. echo just outputs to the standard output (descriptor 1), which your shell made point to /var/tmp/testfile1. Try strace -ff sh -c "echo > /tmp/somefile" and you'll see /tmp/somefile is being open for writing.
You missed the open64 function

polyfit/polyval with log scale through scatter points in matlab

I have a scatter plot with both x and y axes in log scale in Matlab. How do I add a line of best fit on the log scale?
Thanks!
x = [0.0090 0.0000 0.0001 0.0000 0.0001 0.0000 0.0097 0.0016 0.0006 0.0000 0.0016 0.0013 0.0023];
y = [0.0085 0.0001 0.0013 0.0006 0.0005 0.0006 0.0018 0.0076 0.0015 0.0001 0.0039 0.0015 0.0024];
scatter(x,y)
set(gca,'YScale','log');
set(gca,'XScale','log');
hold on
p = polyfit(log(x),log(y),1);
f = polyval(p,x);
plot(x,f,'Color',[0.7500 0.7500 0.7500],'linewidth',2)
When searching for the best fit, you need to use the original data x and y and not their logs. The log scale serves only for representation of the result.
Before use the polyval you need to sort the x. It does not matter when using normal axes, but can look strange with log-axes, because of the wrong sequence.
Here is the plot:
The code:
x = [0.0090 0.0000 0.0001 0.0000 0.0001 0.0000 0.0097 0.0016 0.0006 0.0000 0.0016 0.0013 0.0023];
y = [0.0085 0.0001 0.0013 0.0006 0.0005 0.0006 0.0018 0.0076 0.0015 0.0001 0.0039 0.0015 0.0024];
scatter(x,y);
set(gca,'YScale','log');
set(gca,'XScale','log');
hold on;
x_sort = sort(x);
p = polyfit(x,y,1);
f = polyval(p,x_sort);
plot(x_sort,f,'Color',[0.7500 0.7500 0.7500],'linewidth',2);
Is it what you wanted?

How to vectorize this code involving matrix pages in MATLAB?

Is it possible to vectorize, and possibly run on a GPU, the following code
x = linspace(0,100,1000);
h = zeros(size(x));
for i = 1 : length(x)
exprho = expm(-x(i)*rho);
h(i) = trace(drho*exprho*drho*exprho);
end
out = 2 * trapz(x,h);
where rho and drho are two complex Hermitian square matrices of the same size. rho is in fact a quantum density matrix and drho is its derivative with respect to a parameter.
The size can range from 10 x 10 to 300 x 300 approximately but I would also like to reach bigger sizes.
Here are two sample matrices:
rho =
0.4046 0.3849 0.2589 0.1422 0.0676 0.0288 0.0112 0.0040 0.0014 0.0004 0.0001
0.3849 0.3661 0.2462 0.1352 0.0643 0.0274 0.0106 0.0038 0.0013 0.0004 0.0001
0.2589 0.2462 0.1656 0.0910 0.0433 0.0184 0.0071 0.0026 0.0009 0.0003 0.0001
0.1422 0.1352 0.0910 0.0500 0.0238 0.0101 0.0039 0.0014 0.0005 0.0002 0.0000
0.0676 0.0643 0.0433 0.0238 0.0113 0.0048 0.0019 0.0007 0.0002 0.0001 0.0000
0.0288 0.0274 0.0184 0.0101 0.0048 0.0020 0.0008 0.0003 0.0001 0.0000 0.0000
0.0112 0.0106 0.0071 0.0039 0.0019 0.0008 0.0003 0.0001 0.0000 0.0000 0.0000
0.0040 0.0038 0.0026 0.0014 0.0007 0.0003 0.0001 0.0000 0.0000 0.0000 0.0000
0.0014 0.0013 0.0009 0.0005 0.0002 0.0001 0.0000 0.0000 0.0000 0.0000 0.0000
0.0004 0.0004 0.0003 0.0002 0.0001 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
0.0001 0.0001 0.0001 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
drho =
0.0366 0.0156 -0.0025 -0.0085 -0.0074 -0.0046 -0.0023 -0.0010 -0.0004 -0.0002 -0.0001
0.0156 -0.0035 -0.0147 -0.0148 -0.0103 -0.0057 -0.0028 -0.0012 -0.0005 -0.0002 -0.0001
-0.0025 -0.0147 -0.0181 -0.0145 -0.0091 -0.0048 -0.0022 -0.0009 -0.0004 -0.0001 -0.0000
-0.0085 -0.0148 -0.0145 -0.0105 -0.0062 -0.0031 -0.0014 -0.0006 -0.0002 -0.0001 -0.0000
-0.0074 -0.0103 -0.0091 -0.0062 -0.0035 -0.0017 -0.0008 -0.0003 -0.0001 -0.0000 -0.0000
-0.0046 -0.0057 -0.0048 -0.0031 -0.0017 -0.0008 -0.0004 -0.0001 -0.0001 -0.0000 -0.0000
-0.0023 -0.0028 -0.0022 -0.0014 -0.0008 -0.0004 -0.0002 -0.0001 -0.0000 -0.0000 -0.0000
-0.0010 -0.0012 -0.0009 -0.0006 -0.0003 -0.0001 -0.0001 -0.0000 -0.0000 -0.0000 -0.0000
-0.0004 -0.0005 -0.0004 -0.0002 -0.0001 -0.0001 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000
-0.0002 -0.0002 -0.0001 -0.0001 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000
-0.0001 -0.0001 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000

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.

Why am I getting a matrix of zeros and infs when dividing matrices of the same dimension by element?

I'm trying to get two matrices to divide, properly, element by element.
Essentially, firstd is a 6x499 and secd is 6x498. I first eliminate firstd's extra elements by doing firstd(:,499)=[]; making it 6x498. Now the next step is to transform firstd into the nominator, nom=((firstd.^2)+1).^1.5; My denominator is just denom=secd;
Both nom and denom have come out as 6x498 matrices with real, non-zero data for each element. However, when doing Rlayer=nom./denom, Rlayer comes out as this ludicrous 6x498 zero-ridden matrix.
I also trimmed out the elements in denom that were =0 by changing them to 0.0001.
Segment of result for Rlayer (Columns 493 through 498)
-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.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
Below are two segments of denom (Columns 487 through 492)
0.0250 0.0281 -0.0281 0.0125 -0.0500 0.0969
-0.0125 0.0750 -0.1219 0.1094 -0.0938 0.0937
0.0344 0.0406 -0.1094 0.1187 -0.1344 0.1531
0.0001 0.0250 0.0001 -0.0437 0.0500 0.0062
0.0781 -0.0219 0.0094 -0.0125 -0.0188 0.1062
0.0250 0.0438 -0.0812 0.0937 -0.1063 0.1562
(Columns 493 through 498)
-0.1187 0.1156 -0.0844 0.0688 -0.0406 0.0125
-0.0969 0.1094 -0.0906 0.0469 0.0062 -0.0156
-0.1375 0.1719 -0.1656 0.0781 0.0187 -0.0531
-0.0562 0.1188 -0.1500 0.1438 -0.1187 0.1187
-0.1781 0.2281 -0.2156 0.1750 -0.1250 0.0812
-0.1750 0.1938 -0.1469 0.0563 0.0031 -0.0156
and this is a segment of nom (Columns 493 through 498)
1.0904 1.0235 1.0881 1.0368 1.0769 1.0514
1.0685 1.0201 1.0769 1.0272 1.0497 1.0532
1.0928 1.0180 1.1210 1.0201 1.0568 1.0685
1.0568 1.0285 1.1001 1.0170 1.0952 1.0260
1.0952 1.0078 1.1380 1.0107 1.1026 1.0272
1.0928 1.0078 1.1077 1.0212 1.0463 1.0480
Why is this division leading to this result? I've tried dividing with rdivide, in a double for loop, and row by row in a for loop. All number types are double.