I am very confused of how to use the output of FFD , here is a example output generate by
result = np.abs(fftpack.fft(targetArray))[0:sample_size/2]
print result will gives a ndarray :
[ 4.21477326e+05 3.03444591e+04 1.80682694e+04 1.05924224e+04
1.98799134e+04 2.82620258e+04 1.39538936e+04 2.40331051e+04
4.57465472e+04 6.41241587e+04 1.88203479e+04 1.88670469e+04
5.42137198e+03 5.97675834e+03 1.23986101e+04 9.70841580e+02
2.07380817e+04 4.52632516e+04 4.49493295e+04 1.03481720e+04
2.09126074e+04 2.07691720e+04 1.22091133e+04 1.85574554e+04
1.70652450e+04 2.53329892e+04 3.33015423e+04 2.80037092e+04
2.22665347e+04 2.08644916e+04 1.76186194e+04 1.14038308e+04
3.10918574e+04 2.97875178e+04 5.85659852e+04 3.77721316e+04
1.78952068e+04 1.74037254e+04 1.80938450e+04 3.05330693e+04
4.24407000e+04 2.74864012e+04 4.41637099e+04 3.31359768e+04
1.74327061e+04 1.06382371e+04 2.49780963e+04 1.66725706e+04
8.29635786e+03 2.58186162e+04 1.16012033e+04 2.63763332e+04
1.57059736e+04 1.70297649e+04 1.03524047e+04 3.25882139e+03
5.26665772e+03 1.15851816e+04 1.28794168e+04 1.01768783e+04
8.62533363e+03 4.42905399e+03 4.23319771e+03 9.19616531e+03
4.25165576e+03 4.96332367e+03 6.89522891e+03 4.04350194e+03
1.44337315e+04 7.78488536e+03 5.85210341e+03 3.56468734e+03
8.35331002e+03 5.41229478e+03 4.55374300e+03 1.01986739e+04
7.04839030e+03 3.78646612e+03 7.07872186e+03 4.06017111e+03
4.63905900e+03 9.84305110e+03 6.24621254e+03 2.76105428e+03
3.23469868e+03 2.31035716e+03 2.15636401e+03 2.60730520e+03
3.77829898e+03 3.40698553e+03 8.72582345e+02 1.72800115e+03
1.83439871e+03 7.95636998e+03 4.69696233e+03 2.50245035e+03
6.50413681e+03 4.75657064e+03 3.18621272e+03 1.38365470e+03
6.25551674e+02 3.51399477e+03 2.96447293e+03 1.88665733e+03
1.63347429e+03 1.92721372e+03 5.34091697e+03 2.86529694e+03
1.80361382e+03 3.65567684e+03 2.08391205e+03 3.77302704e+03
2.61882954e+03 1.17689735e+03 1.10303601e+03 1.46603669e+03
1.67959657e+03 1.90800388e+03 2.35782546e+03 1.61309844e+03
1.36326484e+03 4.06967773e+03 1.40142207e+03 1.32657523e+03
3.17829657e+03 2.48240862e+03 1.84764188e+03 2.46198195e+03
2.44352793e+03 1.29546145e+03 9.34633855e+02 1.42411185e+02
1.11686208e+03 1.61629862e+03 1.82113405e+03 1.26350347e+03
3.63268437e+03 9.33373272e+02 8.45292645e+02 1.03929325e+03
1.65583031e+03 9.54310546e+02 1.95195173e+03 1.91535953e+03
5.61485427e+02 1.98666296e+03 9.88850958e+02 7.80781362e+02
1.16064386e+03 1.08425676e+03 3.95616137e+02 1.25423006e+03
2.12467757e+03 7.12337370e+02 1.44060716e+03 7.73146781e+02
1.05641593e+03 1.19763314e+03 1.59583780e+03 1.23434921e+03
3.33146158e+02 1.75650022e+03 8.81978933e+02 1.28186954e+03
1.47573928e+03 8.07757403e+02 8.84292001e+02 1.64624690e+03
1.29680496e+03 4.76763593e+02 1.14002526e+03 1.88558087e+02
6.21497355e+02 5.30485958e+02 1.14902281e+03 4.16705689e+02
1.46212548e+03 1.32165278e+03 7.72060051e+02 9.39714410e+02
1.09011170e+03 8.90859235e+02 7.67129975e+02 2.72632265e+02
2.71574309e+02 5.28939138e+02 5.04479312e+02 4.53129779e+02
7.42214724e+02 2.61798368e+02 4.98990728e+02 6.02745861e+02
9.87830434e+02 2.97161466e+02 1.08718023e+03 5.87366849e+02
3.00425885e+02 8.33291219e+02 1.31052224e+02 2.31099067e+02
6.64652156e+02 1.32180021e+02 2.92862313e+00 2.39475444e+02
7.71773465e+02 8.34334863e+02 7.92791780e+02 6.70180885e+02
5.73451905e+02 4.66006885e+02 9.48437277e+02 7.04566875e+02
2.54136392e+02 4.29167074e+02 2.69560662e+02 6.08905902e+02
1.04487371e+03 5.70108773e+02 5.03504459e+02 7.67808997e+02
4.38126513e+02 7.56769864e+02 7.36892665e+02 5.61631429e+02
8.44062274e+02 8.30259267e+02 3.41959075e+02 4.06049010e+02
1.68799150e+02 7.98590743e+02 5.24271279e+02 4.96069745e+02
5.49647172e+02 7.41309283e+02 9.07897622e+02 1.04985345e+03
1.00181109e+03 5.42974899e+02 7.35959741e+02 4.04694642e+02
5.81271022e+02 2.01778038e+02 6.00141101e+02 3.80334242e+02
6.44350585e+02 8.54890120e+02 7.12173695e+02 8.64161173e+02
7.57346370e+02 7.92985369e+02 7.39425694e+02 4.64160309e+02
7.04501040e+02 4.39166237e+02 1.01374899e+03 7.39703012e+02
8.22200001e+02 4.71396567e+02 8.06529692e+02 7.18184947e+02
7.04886010e+02 6.71256922e+02 5.19651471e+02 9.20043821e+02
7.69576193e+02 8.78863865e+02 1.09071085e+03 9.10790235e+02
6.99356743e+02 9.75210348e+02 7.42159855e+02 2.94034843e+02
6.98690944e+02 7.64206208e+02 6.88827262e+02 5.81514517e+02
1.00230881e+03 7.13219427e+02 8.59968358e+02 8.52206990e+02
4.52436732e+02 6.05729013e+02 8.60630471e+02 7.77693596e+02
6.06655413e+02 7.24578627e+02 6.57839491e+02 6.72231281e+02
7.01971817e+02 4.12298654e+02 6.04044947e+02 6.71707719e+02
6.30927816e+02 7.82746860e+02 7.94808478e+02 5.94066021e+02
6.51161261e+02 7.95649076e+02 2.92195286e+02 4.08585488e+02
6.10540227e+02 5.15197819e+02 5.67327416e+02 5.21334315e+02
4.52410192e+02 7.44553730e+02 6.98824805e+02 7.93759345e+02
5.97743322e+02 5.74907952e+02 3.85973511e+02 3.58967385e+02
5.79438559e+02 4.50199311e+02 4.60028768e+02 4.84243380e+02
7.86184753e+02 4.04682342e+02 5.55837013e+02 6.36922370e+02
3.40645318e+02 5.57139578e+02 3.69777174e+02 3.78496601e+02
5.39000001e+02 8.90982470e+02 3.24737044e+02 2.77411040e+02
4.87813362e+02 1.67412278e+02 7.61243559e+02 3.58371802e+02
5.23608891e+02 2.89915508e+02 5.71091257e+02 6.46835815e+02
4.49435858e+02]
I want use this array to calculate FFT energy , but these numbers doesn't looks like a complex number ..
I want write a function like this :
def get_energy(input):
energy = 0
for e in input:
energy = energy + sqrt(pow(e.real,2)+pow(e.imag,2))
return energy
(I am not very familiar with python language , sorry about this non-pythonic coding)
Many thanks ..
At first, your result does not look like a complex FFT output, because you calculated the absolute values of the FFT. That's never complex...
Therefore:
result = fftpack.fft(targetArray)[0:sample_size//2]
(sample_size//2 ensures that the upper bound of the slice is an integer.)
Your get_energy-function should actually look like this:
def get_energy(input):
return np.sum(input*input.conj())
The energy of a signal is the sum of the squared amplitudes... alternatively you can write it as
def get_energy(input):
return np.sum(np.abs(input)**2)
Note that in numpy mathematical operations are executed elementwise. So you do not have to square each of the element in a for-loop.
Related
I am trying to implement the Hamming distance in Python. Hamming distance is typically used to measure the distance between two codewords. The operation is simply performing exclusive OR. For example, if we have the codewords 10011101 and 10111110, then their exclusive OR would be 00100011, and the Hamming distance is said to be 1 + 1 + 1 = 3.
My code is as follows:
def hamming_distance(codeword1, codeword2):
"""Calculate the Hamming distance between two bit strings"""
assert len(codeword1) == len(codeword2)
x, y = int(codeword1, 2), int(codeword2, 2) # '2' specifies that we are reading a binary number
count, z = 0, x^y
while z:
count += 1
z &= z - 1
return count
def checking_codewords(codewords, received_data):
closestDistance = len(received_data) # set default/placeholder closest distance as the maximum possible distance.
closestCodeword = received_data # default/placeholder closest codeword
for i in codewords:
if(hamming_distance(i, received_data) < closestDistance):
closestCodeword = i
closestDistance = hamming_distance(i, received_data)
return closestCodeword
print(checking_codewords(['1010111101', '0101110101', '1110101110', '0000000110', '1100101001'], '0001000101'))
hamming_distance(codeword1, codeword2) takes the two input parameters codeword1 and codeword2 in the form of binary values and returns the Hamming distance between the two input codewords.
checking_codewords(codewords, received_data) should determine the correct codeword IFF there are any errors in received data (i.e., the output is the corrected codeword string). Although, as you can see, I haven't added the "IFF there are any errors in received data" part yet.
I just tested the checking_codewords function with a set of examples, and it seems to have worked correctly for all of them except one. When I use the set of codewords ['1010111101', '0101110101', '1110101110', '0000000110', '1100101001'] and the received data '0001000101' the output is 0101110101, which is apparently incorrect. Is there something wrong with my code, or is 0101110101 actually correct and there is something wrong with the example? Or was this just a case where there was no error in the received data, so my code missed it?
For my point of view, is not clear why your algorithm transforms the initial string into an integer to do a bitwise difference.
I mean, after the assert the equal length you can simply compute the difference using the zip function:
sum([c1!=c2 for c1,c2 in zip(codeword1,codeword2)])
For sum function, python consider True==1, False==0.
Doing a little simplification on your code:
def hamming_distance(codeword1, codeword2):
"""Calculate the Hamming distance between two bit strings"""
assert len(codeword1) == len(codeword2)
return sum([c1!=c2 for c1,c2 in zip(codeword1,codeword2)])
def checking_codewords(codewords, received_data):
min_dist, min_word = min([(hamming_distance(i, received_data), received_data) for i in codewords])
return min_word
print(checking_codewords(['1010111101', '0101110101', '1110101110', '0000000110', '1100101001'], '0001000101'))
I have an array of time values. I want to know how many values are in each 0.05 seconds window.
For example, some values of my array are: -1.9493, -1.9433, -1.911 , -1.8977, -1.8671,..
In the first interval of 0.050 seconds (from -1.9493 to -1.893) I´m expecting to have 3 elements
I already create another array with the 0.050 seconds steps.
a=max(array)
b=min(array)
ventanalinea1=np.arange(b,a,0.05)
v1=np.array(ventanalinea1)
In other words, I would like to compare my original array with this one.
I would like to know if there is a way to ask python to evaluate my array within a given dynamic range.
One of the variants:
import numpy as np
# original array
a = [-1.9493, -1.9433, -1.911 , -1.8977, -1.8671]
step = 0.05
bounds = np.arange(min(a), max(a) + step, step)
result = [
list(filter(lambda x: b[i] <= x <= b[i+1], a))
for i in range(len(b)-1)
]
I have found a cool python library python-intervals that simplify your problem a lot:
Install it with pip install python-intervals and try the code below.
import intervals as I
# This is a recursive function
def counter(timevalues, w=0.050):
if not timevalues:
return "" # stops recursion when timevalues is empty
# Make an interval object that provides convenient interval operations like 'contains'
window = I.closed(
timevalues[0], timevalues[0] + w)
interval = list(
filter(window.contains, timevalues))
count = len(interval)
timevalues = timevalues[count:]
print(f"[{interval[0]} : {interval[-1]}] : {count}")
return counter(timevalues)
if __name__ == "__main__":
times = [-1.9493, -1.9433, -1.911, -1.8977, -1.8671]
print(counter(times))
Adapt it as you wish, for example you might want to return a dictionary rather that a string.
You could still get around this without using the python-intervals library here but i have introduced it here because it will be very likely that you would need other complex operations along the way on your code.
I'm looking for a simple solution to perform multi-factor ANOVA analysis in python. A 2-factor nested ANOVA is what I'm after, and the SPM1D python module is one way to do that, however I am having an issue.
http://www.spm1d.org/doc/Stats1D/anova.html#two-way-nested-anova
for any of the nested approach examples, there is never any F-statistic or p_values printed, nor can I find any way to print them or send them to a variable.
To go through the motions of running one of their examples, where B is nested inside A, with Y observations:
import numpy as np
from matplotlib import pyplot
import spm1d
dataset = spm1d.data.uv1d.anova2nested.SPM1D_ANOVA2NESTED_3x3()
Y,A,B = dataset.get_data()
#(1) Conduct ANOVA:
alpha = 0.05
FF = spm1d.stats.anova2nested(Y, A, B, equal_var=True)
FFi = FF.inference(0.05)
print( FFi )
#(2) Plot results:
pyplot.close('all')
FFi.plot(plot_threshold_label=True, plot_p_values=True)
pyplot.show()
The only indication of statistical significance provided is whether the h0 hypothesis is rejected or not.
> print( FFi )
SPM{F} inference list
design : ANOVA2nested
nEffects : 2
Effects:
A z=(1x101) array df=(2, 6) h0reject=True
B z=(1x101) array df=(6, 36) h0reject=False
In reality, that should be enough. However, in science, scientists like to think of something as more or less significant, which is actually kind of crap... significance is binary. But that's how they think about it, so I have to play along in order to get work published.
The example code produces a matplotlib plot, and this DOES have the f statistic and p_values on it!
#(2) Plot results:
pyplot.close('all')
FFi.plot(plot_threshold_label=True, plot_p_values=True)
pyplot.show()
But I can't seem to get any output which prints it.
FFi.get_p_values
and
FFi.get_f_values
produce the output:
<bound method SPMFiList.get_p_values <kabammi edit -- or get_f_values> of SPM{F} inference list
design : ANOVA2nested
nEffects : 2
Effects:
A z=(1x101) array df=(2, 6) h0reject=True
B z=(1x101) array df=(6, 36) h0reject=False
So I don't know what to do. Clearly the FFi.plot class can access the p_values (with plot_p_values) but FFi.get_p_values cant!!? Can anyone lend a hand?
cheers,
K
The easiest way to get the p values is to use the get_p_values method that you mention, you just need to call the method by adding () to the end.
p = FFi.get_p_values()
print(p)
This yields:
([0.016584151119287904], [])
To see more detailed information for each effect in 2+-way ANOVA, including p values, use print along with the individual F statistics like this:
print( FFi[0] )
print( FFi[1] )
The first print statement will produce output like this:
SPM{F} inference field
SPM.effect : Main A
SPM.z : (1x101) raw test stat field
SPM.df : (2, 6)
SPM.fwhm : 11.79254
SPM.resels : (1, 8.47993)
Inference:
SPM.alpha : 0.050
SPM.zstar : 24.30619
SPM.h0reject : True
SPM.p_set : 0.017
SPM.p_cluster : (0.017)
You can retrieve the clusters' p values like this:
p = [F.p for F in FFi]
which gives the same result as calling get_p_values.
Note that there are no p values in this case for FFi[1] because the test statistic fails to cross the alpha-defined threshold (see the "Main B" panel in the figure above). If you need to report p values in this case as well, one option is simply to use "p > alpha". More precise p value are available parametrically up until about p = 0.5, but larger p values than that are not very accurate using parametric methods, so if you need p values for all cases consider using the nonparametric version: spm1d.stats.nonparam.anova2nested.
I had a very strange problem in the minimize function. I wrote following code and hoped to output result from (0,1).
cons = ({'type':'eq','fun':lambda x: 1-sum(x)})
bnds = [(0,1)]*len(w)
minS = minimize(min_S_function, w, method = 'SLSQP', bounds = bnds, constraints = cons)
However, the result output many extremely small numbers instead of zero even though I set bounds between 0 to 1. Why is that?
In [68]:minS.x
Out[68]:
array([ 2.18674802e-14, -2.31905438e-14, 4.05696128e-01,
1.61295198e-14, 4.98954818e-02, -2.75073615e-14,
3.97195447e-01, 1.09796187e-14, -4.33297358e-15,
2.38805100e-14, 7.73037793e-15, 3.21824430e-14,
-1.42202909e-14, -1.08110329e-14, -1.83513297e-14,
-1.37745269e-14, 3.37854385e-14, 4.69473932e-14,
-1.09088800e-15, -1.57169147e-14, 7.47784562e-02,
1.32782180e-02, 1.64441640e-14, 2.72140153e-15,
5.23069695e-14, 5.91562687e-02, 2.16467506e-15,
-6.50672519e-15, 2.53337977e-15, -6.68019297e-14])
This is an acceptable solution!
Those iterative solvers only guarantee a local-optimum approximate-solution and that's what you got!
Check your numbers and you will see, that the distance to the lower-bound (of your negative out-of-bound values) is within 10^-14 = 0,00000000000001, an acceptable error (we use floating-point math after all).
I am trying to implement a simple formula with python . In the case of "RLSR" there two approach to calculate the coefficient "C" by C=k^-1*y where k is the kernel matrix and y is the target value vector.
The second way is by using eigenvalues(w) and eigenvectors(v) of the kernel method and using formula :
Sum(u(i)*<u(i),y> / w(i) ).
to check the answer of the result I'm checking whether y=kc or not! in the first case it's ok , but the latter case it's not.but the problem is my c vectors in both method look the same.
output from invers algorithm :
[ 19.49840251 18.82695226 20.08390355 15.01043404 14.79353281
16.75316736 12.88504257 16.92127176 16.77292954 17.81827473
20.90503787 17.09359467 18.76366701 18.14816903 20.03491117
22.56668264 21.45176136 25.44051036 30.40312692 22.61466379
22.86480382 19.34631818 17.0169598 19.85244414 16.63702471
20.35280156 20.58093488 22.42058736 20.54935198 19.35541575
20.39006958 19.74766081 20.41781019 22.33858797 17.57962283
22.61915219 22.54823733 24.96292824 22.82888425 34.18952603
20.7487537 24.82019935 22.40621769 21.15767304 27.58919263
18.39293156 21.55455108 18.69532341]
output from second(eigendecomposition ) algorithm :
[ 19.25280289 18.73927731 19.77184991 14.7650427 14.87364331
16.2273648 12.29183797 16.52024239 16.66669961 17.59282615
20.83059115 17.02815857 18.3635798 18.16931845 20.50528549
22.67690164 21.40479524 25.54544 30.94618128 22.72992565
23.06289609 17.6485592 15.2758427 19.50578691 16.45571607
20.19960765 20.35352859 22.60091638 20.36586912 18.90760728
20.57141151 19.43677153 20.43437031 22.39310576 17.72296978
22.82139991 22.50744791 25.10496617 22.30462867 34.80540213
20.77064617 25.18071618 22.5500315 20.71481252 27.91939784
18.29868659 22.00800019 18.71266093]
this how I have implemented it:
so let say if we have 48 samples the size of K is 48x48 , y is 48x1
def cpu_compute(y,k):
w, u=LA.eigh(k)
uDoty=np.dot(u,y)
temp=np.transpose(np.tile(w,(len(u),1)))
div=np.divide(u,temp)
r=np.tile(uDoty,(len(div),1))
a=div*r.T
c=sum(a)
return c
and the result from
print np.allclose(Y,np.dot(K,c))
is false.
also the norm of difference from the true result is 3.13014997999.
now I have no idea how can I fix this . I thought maybe some how by doing more precise answer.
Appreciate any help!
To solve k c = y with numpy, use numpy.linalg.solve:
c = solve(k, y)
This uses an algorithm that is much more robust than the methods you are trying.