I've found several graphing libraries for android which would be very suitable for my school project e.g. GraphView
My task is to display 3 Biorhythm curves on one graph. I looked at many examples and helps but I just couldn't get my head around how to change the code so it will display my biorhythm curve.
// sin curve
int num = 150;
GraphViewData[] data = new GraphViewData[num];
double v = 0;
for (int i = 0; i < num; i++) {
v += 0.2;
data[i] = new GraphViewData(i, Math.sin(v));
}
The three biorhythm cycle equations are:
physical: sin(2\pi t/23),
emotional: sin(2\pi t/28),
intellectual: sin(2\pi t/33),
t = number of days since birth, this number has been calculated from the users input and is stored in a shared preference defined as differenceInDays
It would be awesome if someone could just give me an example, please do ask if I need to be more specific (this is my first time posting on this website) and if you have a suggestion to THE perfect graphing library in android please let me know :D
Related
I tried write an automation for my games in Android with Tasker and AutoTools plugins. Somethings ok at this point but i need capture the screenshot and need interpret it for my needs.
That's exactly what I need;
Some texts are important in games and i want to click on it wherever they are in the screen. So i need OCR for this task i think. I follow some solutions but fail or stuck everytime. Let me explain which solutions i tried.
Following Solution 1:
I tried AutoInput (Tasker plugin) UIQuery method but fail. Because UIQuery of AutoInput just works on android UI i think. Cant get any information from 3D App like games.
Following Solution 2:
I search OCR solution and find AutoTools (Tasker plugin)
Create a task and take screenshot and interpret it with AutoTools OCR method. Thats ok. AutoTools OCR succesfully read a text from Image file.
But i stuck again. Because i succesfully read a text from image file but i dont know x y coordinate of important text.
What suggest at this point?
Should i learn android and write own app?
You should checkout out the ocr-reader Google sample. It's quick to run and not too difficult to get what you're looking for. What you would need to do is modify the OcrDetectorProcess that comes with the sample to break down the text into individual words, then you can easily calculate the boundaries and center points of each word. Here's some code to get you started:
#Override
public void receiveDetections(Detector.Detections<TextBlock> detections) {
mGraphicOverlay.clear();
// Get all detected items.
SparseArray<TextBlock> items = detections.getDetectedItems();
for (int i = 0; i < items.size(); ++i) {
TextBlock item = items.valueAt(i);
// Get individual lines in each item.
List<Line> lines = (List<Line>) item.getComponents();
for (Line line : lines) {
// Get individual "words" in each line.
List<Element> elements = (List<Element>) line.getComponents();
for (Element e : elements) {
// Now get the position of each element.
Rect rect = e.getBoundingBox();
Point[] points = e.getCornerPoints();
int centerX = (points[0].x + points[2].x) / 2;
int centerY = (points[0].y + points[2].y) / 2;
// DO STUFF
}
}
}
}
I contact with the developer who write the "AutoTools" Tasker plugin.
He/She add some function to plugin and solve it.
Plugin, interpret with OCR granted image and return words and center of xy positions of each words now.
If anyone search like this function for Android and Tasker App please visit this forum topic link. Its very useful.
I am working on a requirement to display the luma value of raw YUV image(1280 x 720) as graph. That is, I am separating the Y data and displaying it in the form of graph, in which x axis is the width and the y axis is the respective Y value.
// Code
int count = 0;
int byteValue = 0;
for ( y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
byteValue = pPictureIn[count++] & 0xff;
series.addLast(x, byteValue);
}
}
final PlotStatistics stats = new PlotStatistics(10, false);
plot.addListener(stats);
redrawer = new Redrawer(Arrays.asList(new Plot[]{plot}),
1, false);
format = new LineAndPointFormatter(this, R.xml.formatter);
plot.addSeries(series, format);
redrawer.start();
I am using AndroidPlot to plot the graph. And I am adding all the points to the series. Here my problem is, if I try to render the points, My app gets freezed. And I am using the render mode as USE_BACKGROUND_THREAD.
Someone please help me to render the points at one shot without any freeze. Thanks in advance
Im going to guess that you're using SimpleXYSeries, which is not optimized for efficiency or speed; the calls to addLast become extremely expensive as the number of points increase. Using a fixed memory XYSeries implementation will provide far better performance. If your image data is dynamic (coming from a camera or some other image stream) then a ring buffer might be a good design to consider...I'd suggest taking a look at FixedSizeEditableXYSeries in particular.
Additionally, you might consider sampling your data to reduce the size using SampledXYSeries.
The Advanced XY Series Types doc has more details about the pros and cons of the above mentioned classes and a few others.
I am trying to develop a android runner game for school purpose..
I am still new to this and please I need your assistance..
You guys can view my CS5flash file at >>> http://www.filedropper.com/test_37
The obstacles and coins are on random. But the obstacles and coins are overlapping each other.. Which is very bad for a runner game because it looks very bad and the gameplay gets very very complicated.
How can i fix it??. Is there any way to fix it?.
And i am also thinking if I can set the obstacles and coins to a specific area (not on random). So the game will be more oganized and the gameplay won't be complicated. Which i still don't know -_-.
But i still prefer it on random. So guys please help me fix it..
You will need to change the way you are adding the coins and obstacles! I suggest using a timer for each. Atm you are adding a ton of them on every frame, calculating overlaps would use too much resources! and put them in an array or better a vector! i would reccomend using an object Pool aswell!
so limit the amout of coins and hurdles that can be present, like 5 or so. then remove them from the array/vector when they are offscreen or collected! then when you add new stuff you can check against the array/vector what the allowed values are!
when you got your Array you can pass it to the randomRange() function and exlcude those values!
would look somthing like this! not testet!!
function randomRange (min:Number, max:Number, exclude:Array = null):int
{
var val:int = (min + Math.random() * (max - min)) >> 0;
if (exclude)
{
for (var i:int = 0; i < exclude; i++)
{
while ((val < exclude[i].x + exclude[i].width) && (val > exclude[i].x))
{
val = (min + Math.random() * (max - min)) >> 0;
}
}
}
return val;
}
Its still quite exspensive performance wise. but with only a few object you should be fine
I am developing an Android app and I am need to detect user context (if walking or driving at minimal)
I am using accelerometer and sum of all axes to detect the accleration vector. It is working pretty well in the way I can see some periodics values while walking. But I need to detect these poeriods programmatically.
Please is there any kind of math function to detect period in set of values? I heard Fourier transformation is usable for that, but I really dont know how to implement it. It looks pretty complicated :)
Please help
The simplest way to detect periodicity of data is autocorrelation. This is also fairly simple to implement. To get the autocorrelation at i you simply multiply each data point of your data with each data point shifted by i. Here is some pseudocode:
for i = 0 to length( data ) do
autocorrel[ i ] = 0;
for j = 0 to length( data ) do
autocorrel[ i ] += data( j ) * data( ( j + i ) mod length( data ) )
done
done
This will give you an array of values. The highest "periodicity" is at the index with the highes value. This way you can extract any periodic parts (there usually is more than one).
Also I would suggest you do not try implement your own FFT in an application. Although this algorithm is very good for learning, there is much one can do wrong which is hard to test and it is also likely that your implementation will be much slower than those which are already available. If it is possible on your system I would suggest you use the FFTW which is impossible to beat in any respect, when it comes to FFT implementations.
EDIT:
Explanation, why this works even on values which do not repeat exactely:
The usual and fully correct way to calculate the autocorrelation is, to substract the mean from your data. Let's say you have [1, 2, 1.2, 1.8 ]. Then you could extract 1.5 from each sample leaving you with [-.5, .5, -.3, .3 ]. Now if you multiply this with itself at an ofset of zero, negatives will be multiplied by negatives and positives by positives, yielding (-.5)^2 + (.5)^2 + (-.3)^2 + (.3)^2=.68. At an offset of one negatives will be multiplied with positives yielding (-.5)*(.5) + (.5)*(-.3) + (-.3)*(.3) + (.3)*(-.5)=-.64. At an offset of two again negatives will be multiplied by negatives and positives by positives. At offset of three something similar to the situation for an offset of one happens again. As you can see, you get positive values at offsets of 0 and 2 (the periods) and negative values at 1 and 4.
Now to only detect the period it is not necessary to substract the mean. If you just leave the samples as-is, the suqared mean will be added at each addition. Since the same value will be added for each calculated coefficient, the comparison will yield the same results as if you first subtracted the mean. At worst either your datatype might run over (in case you use some kind of integral type), or you might get round off errors when the values start getting to big (in case you use float, usually this is not a problem). In case this happens first substract the mean and try if your results get better.
The strongest drawback of using autocorrelation vs. some kind of fast fourier transformation is the the speed. Autocorelation takes O(n^2) where as a FFT only takes O(n log(n)). In case you need to calculate the period of very long sequences very often, autocorelation might not work in your case.
If you want to know how the fourier transformation works, and what all this stuff about real part, and imaginary part, magnitude and phase (have a look at the code posted by Manu for example) means, I suggest you have a look at this book.
EDIT2:
In most cases data is neither fully periodic nor fully chaotic and aperiodic. Usually your data will be composed of several periodic compenents, with varying strength. A period is a time difference by which you can shift your data to make it similar to itself. The autocorrelation calculates how similar the data is, if you shift it by a certain amount. Thus it gives you the strength of all possible periods. This means, there is not "index of repeating value", because when the data is perfectly periodic, all indexes will repeat. The index with the strongest value, gives you the shift, at which the data is most similar to itself. Thus this index gives a time offset, not an index into your data. In order to understand this, it is important to understand, how a time series can be thought of as being made up of the sum of perfectly periodic functions (sinusoidal base functions).
If you need to detect this for very long time series, it is usually also best to slide a window over your data and just check for the period of this smaller data frame. However you have to be aware that your window will add additional periods to your data, of which you have to be aware.
More in the link I posted in the last edit.
There is also a way to compute the autocorrelation of your data using FFT which reduces the complexity from O(n^2) to O(n log n). The basic idea is you take your periodic sample data, transform it using an FFT, then compute the power spectrum by multiplying each FFT coefficient by its complex conjugate, then take the inverse FFT of the power spectrum. You can find pre-existing code to compute the power spectrum without much difficulty. For example, look at the Moonblink android library. This library contains a JAVA translation of FFTPACK (a good FFT library) and it also has some DSP classes for computing power spectra. An autocorrelation method I have used with success is the McLeod Pitch Method (MPM), the java source code for which is available here. I have edited a method in the class McLeodPitchMethod which allows it to compute the pitch using the FFT-optimized autocorrelation algorithm:
private void normalizedSquareDifference(final double[] data) {
int n = data.length;
// zero-pad the data so we get a number of autocorrelation function (acf)
// coefficients equal to the window size
double[] fft = new double[2*n];
for(int k=0; k < n; k++){
fft[k] = data[k];
}
transformer.ft(fft);
// the output of fft is 2n, symmetric complex
// multiply first n outputs by their complex conjugates
// to compute the power spectrum
double[] acf = new double[n];
acf[0] = fft[0]*fft[0]/(2*n);
for(int k=1; k <= n-1; k++){
acf[k] = (fft[2*k-1]*fft[2*k-1] + fft[2*k]*fft[2*k])/(2*n);
}
// inverse transform
transformerEven.bt(acf);
// the output of the ifft is symmetric real
// first n coefficients are positive lag acf coefficients
// now acf contains acf coefficients
double[] divisorM = new double[n];
for (int tau = 0; tau < n; tau++) {
// subtract the first and last squared values from the previous divisor to get the new one;
double m = tau == 0 ? 2*acf[0] : divisorM[tau-1] - data[n-tau]*data[n-tau] - data[tau-1]*data[tau-1];
divisorM[tau] = m;
nsdf[tau] = 2*acf[tau]/m;
}
}
Where transformer is a private instance of the FFTTransformer class from the java FFTPACK translation, and transformerEven is a private instance of the FFTTransformer_Even class.
A call to McLeodPitchMethod.getPitch() with your data will give a very efficient estimate of the frequency.
Here is an example of calculating the Fourier Transform android using the FFT class from libgdx:
package com.spec.example;
import android.app.Activity;
import android.os.Bundle;
import com.badlogic.gdx.audio.analysis.FFT;
import java.lang.String;
import android.util.FloatMath;
import android.widget.TextView;
public class spectrogram extends Activity {
/** Called when the activity is first created. */
float[] array = {1, 6, 1, 4, 5, 0, 8, 7, 8, 6, 1,0, 5 ,6, 1,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float[] array_hat,res=new float[array.length/2];
float[] fft_cpx,tmpr,tmpi;
float[] mod_spec =new float[array.length/2];
float[] real_mod = new float[array.length];
float[] imag_mod = new float[array.length];
double[] real = new double[array.length];
double[] imag= new double[array.length];
double[] mag = new double[array.length];
double[] phase = new double[array.length];
int n;
float tmp_val;
String strings;
FFT fft = new FFT(32, 8000);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
fft.forward(array);
fft_cpx=fft.getSpectrum();
tmpi = fft.getImaginaryPart();
tmpr = fft.getRealPart();
for(int i=0;i<array.length;i++)
{
real[i] = (double) tmpr[i];
imag[i] = (double) tmpi[i];
mag[i] = Math.sqrt((real[i]*real[i]) + (imag[i]*imag[i]));
phase[i]=Math.atan2(imag[i],real[i]);
/****Reconstruction****/
real_mod[i] = (float) (mag[i] * Math.cos(phase[i]));
imag_mod[i] = (float) (mag[i] * Math.sin(phase[i]));
}
fft.inverse(real_mod,imag_mod,res);
}
}
More info here: http://www.digiphd.com/android-java-reconstruction-fast-fourier-transform-real-signal-libgdx-fft/
In a similar approach to this question, I am looking for a way to plot data points on to a view in Android. Preferably, a library which will do this for arbitrary-ranged input, as well as allow panning and zooming (via pinch or zoom bar).
Right now, I have subclass-ed a view which does the following normalization:
final int width = View.MeasureSpec.getSize(this.widthMeasureSpec);
final int height = View.MeasureSpec.getSize(this.heightMeasureSpec);
final float factorA = width / (maxA - minA);
final float factorS = height / (maxS - minS);
final float constFactorA = factorA * minA;
final float constFactorS = factorS * minS;
final int dataLength = data.length;
for (int i = 0; i < dataLength; ++i) {
if (i % 2 == 0)
_data[i] = _data[i] * factorA - constFactorA;
else
_data[i] = _data[i] * factorS - constFactorS;
}
and a call in onDraw() to the drawPoints method of a canvas (also, I update this.widthMeasureSpec and this.heightMeasureSpec in onMeasure()).
This is with minA/maxA as the bounds for my independent variable and minS/maxS as the bounds for my dependent variable.
This works fine for displaying the data, but I am hoping someone else has solved the problem of drawing the axes and panning/zooming.
I have ~150,000 data points, and I would prefer to keep these as floats to save half the memory. I don't know how big decimal numbers are in JavaScript, but I really don't want to resort to passing data in through JavaScript for the Google Charts API or an HTML-based solution for memory's sake.
I'm running this on a MyTouch 3g (the original, before 3.5mm jack and it's RAM upgrade), so performance is an issue. I'd like to release the final project under the GPLv3, so this excludes GraphView.
The graphs are of the same type as this, so any optimization by excluding points that are too close together to show up on screen would definitely make a difference.
sargas , do check android-misc-widgets.It contains a widget named PlotView with an example TestInterPolator.
Hope it helps.
Original post: Chart and Graph Library for Android
With the library GraphView it's possible to create a line and bar graphs.
GraphView is a library for Android to programmatically create flexible and nice-looking line and bar diagramms. It is easy to understand, to integrate and to customize it.
First checkout the library and integrate it into your project.
Source code is hosted on github.
GraphView library on github
It's also possible to let the graph be scalable (zooming) and scrollable. More information about this library on Original post: Chart and Graph Library for Android
This is how it will look like:
Then you can easily create it with a few lines of code (see snippet):
// graph with dynamically genereated horizontal and vertical labels
GraphView graphView = new LineGraphView(
this // context
, new GraphViewData[] {
new GraphViewData(1, 2.0d)
, new GraphViewData(2, 1.5d)
, new GraphViewData(2.5, 3.0d) // another frequency
, new GraphViewData(3, 2.5d)
, new GraphViewData(4, 1.0d)
, new GraphViewData(5, 3.0d)
} // data
, "GraphViewDemo" // heading
, null // dynamic labels
, null // dynamic labels
);
LinearLayout layout = (LinearLayout) findViewById(R.id.graph1);
layout.addView(graphView);
There is http://www.jfree.org/jfreechart/ which can suit your needs, it is a Java library so it should fit nice to Android. And it is LGPL.
If you can go the JavaScript route, then you could check out ProtoVis http://vis.stanford.edu/protovis/