Matlab algorithm/workflow need fixing - algorithm

So during work i need to analyise video's like this one
http://youtu.be/TxBdkLcO5Do of a beating cells.
So i wrote a matlab code that plot a graph of changes in picture over time..
example of data of graph:
0 0
0.1 87124
0.15 87124
0.2 87124
0.25 85589
0.3 85589
0.35 85589
0.4 85589
0.45 19202
0.5 19202
0.55 19202
0.6 19202
0.65 61303
0.7 61303
0.75 61303
0.8 61303
0.85 56689
0.9 56689
0.95 56689
1 72988
1.05 72988
1.1 72988
1.15 72988
1.2 63871
1.25 63871
**
How my code works ?
**
left row is time (in fraction of sec.) and 2nd row is amplitude of picture.
I get loop all frames one by one.
Turn frame into gray,calculate threshold,Turn into binary.
Compare each frame with the frame just before it using imabsdiff
store the result in array corresponding with its Frame number / frame rate
And i get my Graph .... (is this a good way or is there a better way to do it by the way ?)
i plot array time,Amplitude_difference
So what i need to do now with this array is to find the number of Peaks that occurs and analyze the Frequency , Strength and regularity of these peaks !
Frequency = how many peaks in video
Strength = Peak summit value-average(2nd row)
regularity = time between each peak and next one !.
So basicly i should create an array peaks in which with every peak i add an element and assign to it value (Time assisted with peak, Strength )
and after this i want to print out a report of number of peaks and the value freq,str,reqularity of it..
:)
HOW THE CAN I DO THAT :D !?

Suppose you just need a peak finder. The rest should be simple.

Related

Animated.Decay - formula to reach value

I like the natural feel decay adds to my animations but the problem I have is that I can't get it to land on a whole number unlike the other animations.
Is there a vector math formula I can use to calc the deceleration value and velocity so that it does?
i.e.
current animated value is 0.75
velocity = 2
Would like to animate to 2.0 so deceleration rate = ???

Netlogo: calculate time using distance traveled and speed

I have a model where I want to measure the time it takes for a turtle to travel at a given speed from one point to another, using the scale of the map and speed/fd movement to quantify elapsed real time.
I have speed set to a random number (global variable input on UI) for all ships and a list for each turtle with the coordinates of the path. I use the formula of time = distance / speed but cannot figure out how to count time accurately.
The speed is set to a number around 30, which I would like to use as km/h. The scale of the map is 1000m x 1000m per patch so I have tried using factors of 1000 to fix the calculation to no avail. below is my calculation for time.
to move
tick
ask ships with [length current-path != 0]
[
let x1 xcor
let x2 [pxcor] of first current-path
let y1 ycor
let y2 [pycor] of first current-path
let distance-traveled sqrt ((x2 - x1)^ 2 + (y2 - y1)^ 2)
set time ((distance-traveled * meters-per-patch) / (speed)
set timelist lput time timelist
set totaltime sum timelist
go-to-next-patch-in-current-path
]
end
This potion calls the movement. I am divding speed/1000 since the number for speed is in the 10's and I want the turtles to move much less than 1 patch each tick to allow it to land directly on the patch coordinates in the list.
to go-to-next-patch-in-current-path
face first current-path
ifelse distance first current-path < .1
[
move-to first current-path
set current-path remove-item 0 current-path
]
[
fd speed / 1000
set heading towards first current-path
]
end
Any help would be greatly appreciated, thanks a lot.
Regardless. If you want someone to move from x1,y1 to x2,y2. You need to calculate the distance to x2 y2. Suppose you want it to take 4 seconds to move from x1,y1 to x2,y2
For a turtle:
on your setup or when you arrive at x1,y1
let t 3
let d distancexy x2 y2
let velocity d / t
facexy x2 y2
while you're not at x2,y2
fd velocity
I think your problem is at every iteration you're recalculating your velocity. You're saying if you're 5 meters away, and want to take 5 seconds, move 1 meter. Then in the next time unit, you'll be 4 meters away and want to take 5 seconds, move .8 meters. etc

Step Based signal to smooth one - How can I interpolate?

I'm programming a sort of audio plugin, and I get an array of values that represent a step-based signal such as this:
that have these values:
[ 0.27, 0.43, 0.48, 0.51, 0.85, 0.15, 0.48, 0.01, 0.28, 0.84, 0.15, 0.22, 0.11, 0.86, 0.66, 0.92, 0.40, 0.71 ]
I'm looking for to transform those values into a bigger array of interpolated values that represent a smooth signal, such as a sine wave. Somethings like this (sorry for my Paint art):
What kind of math should I use here? Inside my development environment (Ruby based), I have a common number of math functions. But I don't know where to start.
What you want here is a digital filter - specifically a lowpass filter.
There are two types of simple digital filter, Finite Impulse Response and Infinite Impulse Response.
A FIR filter works by summing, with some weighting, the previous n samples of the audio, and using that to generate the output sample. It's called "Finite Impulse Response", because a single impulse in the input can only affect a finite number of output samples.
An IIR filter, in contrast, uses its own previous output in addition to the current sample. It's called "Infinite Impulse Response" because of this feedback property; a single impulse can affect all future samples.
Of the two, the IIR filter is the simplest to implement, and in its most basic form looks like this:
state(N) = state(N - 1) * weighting + sample(N)
output(N) = state(N)
That is, for each input sample, reduce the previous state value by some amount and add the input, then use that as the output. As such, it's basically a moving average filter.
For example, if you set 'weighting' to 0.95, then each output sample is influenced 95% by previous samples and 5% by the current sample, and the output value will shift slowly in response to changing inputs. It will also be scaled up by 20X (1/(1-weighting)), so you should renormalize it accordingly.
Here's how the first few steps would work with your input data:
Start by setting state = 20 * 0.27.
Output state / 20 = 0.27
Update state = state * 0.95 + 0.43 = 26.08
Output state / 20 = 0.278.
Update state = state * 0.95 + 0.48 = 5.76
Output state / 20 = 0.288
And so forth. If you need more output data points than input data points, repeat your input samples n times before feeding into the filter, or interleave input samples with n zero samples. Both are valid, though they have different impacts on the filtered output.
There is a lot of theory behind digital filter design; in practice for a simple implementation you can probably use this first-order filter, and adjust the weighting value to suit.

Building A Gaussian Blur?

I am trying to write my own (or at least gain a better understanding of) Gaussian Blur filter using Python 2.7. I would really appreciate some direction. Everywhere else I have looked just uses built-ins...
You need to loop through each pixel in the image. At every pixel take weighted samples from its surroundings and sum them all together to new value of the pixel. So the code would look something like this:
for x in range(input.size[0]):
for y in range(input.size[1]):
result[x, y] = 0
result[x, y] += 0.01 * input[x-1, y+1] + 0.08 * input[x, y+1] + 0.01 * input[x+1, y+1]
result[x, y] += 0.08 * input[x-1, y ] + 0.64 * input[x, y ] + 0.08 * input[x+1, y ]
result[x, y] += 0.01 * input[x-1, y-1] + 0.08 * input[x, y-1] + 0.01 * input[x+1, y-1]
BUT in my code I'm not taking care of the edges of the image. This will result under and over indexing the image. There are at least three different easy ways how to take care of the edges:
You can decrease the range of the for loop so it doesn't blur the pixels on the edge and crop not blurred pixels out of the image after the blur.
You can make if statements in which you check that if you are on the edge of the image. On the edge you are not taking samples out of range and adjusting weights of the other pixels to sum to 1.0.
You can mirror the image to every side. This can be done by actually mirroring the image or by accessing pixels inside the image as far away of the edge as far the over indexing would have gone.
With the options 2 and 3 the edges are not as blurred as the center of the image. This is a minor issue if your sample window size is 3x3 but it can be visible with much bigger sample window sizes.
If you want to achieve good performance, you can try for example replacing the for loops with OpenCL or OpenGL launch and write the inner loop into OpenCL kernel or GLSL shader. These will result as many pixels as possible to be computed in parallel. These can be optimized even further by blurring first in horizontal axes and then in vertical axes, which reduces sample counts and should be faster with bigger sample windows.
About the same thing are explained with other words in this post.

gnuplot matrix or plot : display both color and point value

I'm using gnuplot to analysis data.
And I frequently use palette and matrix.
However whenever I use that, precision is always problem.
If I increase precision by define many color, it is difficult to remember and to read.
If I decrease number of color to clear comparison, precision become decrease.
So I'm thinking matrix with plot number.
If I can display both color and number, it will be more easy to see and analysis.
At least I want display only number,(Just use excel is a one choice but I don't want to)
or display number with different color.(color determined by point value)
If you know the way to do, please let me know.
If you can't understand, please tell me.
Thank you in advance,
To plot the labels, just use the with labels plotting style. You can use any string and string formatting e.g. with sprintf to set the label:
reset
set autoscale fix
set palette defined (0 'white', 1 'green')
set tics scale 0
unset cbtics
set cblabel 'Score'
unset key
plot 'data.txt' matrix with image,\
'' matrix using 1:2:(sprintf('%.2f', $3)) with labels font ',16'
The result with the pngcairo terminal and gnuplot 4.6.3 is:
The data file data.txt for this example is:
0.22 0.13 0.54 0.83 0.08
0.98 0.57 0.52 0.24 0.66
0.23 0.68 0.24 0.89 0.76
0.89 0.78 0.69 0.78 0.10
0.24 0.77 0.27 0.28 0.69
To expand the above answer: One often finds the case, that the labels should have different colors in order to be readable. To produce white labels on a dark background and black labels on bright background I use the following code:
threshold = 123456
f = "..."
plot f matrix with image ,\
'' matrix using 1:2:($3 > threshold ? sprintf('%1.2f', $3) : sprintf("")) with labels tc "white" ,\
'' matrix using 1:2:($3 < threshold ? sprintf('%1.2f', $3) : sprintf("")) with labels tc "black"

Resources