So I'm having the following problem: I have a method that breaks a big matrix into smaller blocks of the same size. After I do some operations on the blocks, I want to reconstruct the big matrix in the right order, but I'm going wrong at it somehow.
The following code reconstructs correctly a 4x4 matrix that breaks into 2x2, but for any other dimensions, it's not working properly.
public long[][] blocksToMatrix(List<long[][]> blocks, int blockDimension, int width, int height ){
long[][] yuvMatrix = new long[height][width];
int heightPos = 0;
int widthPos = 0;
for (int i = 0; i < blocks.size(); i++) {
long[][] yuvBlock = blocks.get(i);
int heightPosTemp = heightPos;
for (int j = 0; j < blockDimension * blockDimension; j++) {
yuvMatrix[heightPos][widthPos] = yuvBlock[j / blockDimension][j % blockDimension];
widthPos++;
if (widthPos >= width){
widthPos = (i * blockDimension) % width;
heightPos++;
}
if (widthPos == ((i + 1) * blockDimension) % width){
widthPos = (i * blockDimension) % width;
heightPos++;
}
}
if (heightPos == height ){
heightPos = heightPosTemp;
}
else {
heightPos = (i * blockDimension) % height;
}
widthPos = ((i + 1) * blockDimension) % width;
}
return yuvMatrix;
}
The method I used to break the matrix:
public List<long[][]> matrixToBlocks(long[][] yuvMatrix, int blockDimension, int width, int height){
int blocksSize = width / blockDimension * (height / blockDimension);
List<long[][]> blocks = new ArrayList<long[][]>();
for (int i = 0; i < blocksSize; i++) {
long[][] subBlock = new long[blockDimension][blockDimension];
int heightPos = (blockDimension * (i / blockDimension)) % height;
int widthPos = (blockDimension * i) % width;
if (widthPos + blockDimension > width) {
widthPos = 0;
}
for (int row = 0; row < blockDimension; row++) {
for (int col = 0; col < blockDimension; col++) {
subBlock[row][col] = yuvMatrix[heightPos + row][col + widthPos];
}
}
blocks.add(subBlock);
}
return blocks;
}
The way I tested it:
public static void testareMatBlo(int height, int width, int blockdim){
long[][] test = new long[height][width];
int val = 1;
for (int i = 0; i < height; i++){
for (int j = 0; j < width; j++){
test[i][j] = val;
val++;
}
}
List<long[][]> blocks = matrixToBlocks(test, blockdim, width, height);
long[][] matrix = blocksToMatrix(blocks, blockdim, width, height);
if (Arrays.deepEquals(test, matrix)){
System.out.println("YES");
}
else {
System.out.println("NO");
}
}
This works:
testareMatBlo(4, 4, 2);
But anything else doesn't. Can anyone explain what I did wrong?
I didn't thoroughly read your code for matrixToBlocks(...) but all those calculations like int blocksSize = width / blockDimension * (height / blockDimension); are very likely to introduce hard to spot errors - and you actually don't need them:
public static List<long[][]> matrixToBlocks(long[][] yuvMatrix, int blockDimension){
//Check matrix and block dimension match
if( yuvMatrix.length == 0 || yuvMatrix.length % blockDimension != 0
|| yuvMatrix[0].length == 0 || yuvMatrix[0].length % blockDimension != 0 ) {
throw new IllegalArgumentException("whatever message you like");
}
List<long[][]> blocks = new ArrayList<long[][]>();
//Iterate over the blocks in row-major order (down first, then right)
for( int c = 0; c < yuvMatrix.length; c += blockDimension ) {
for( int r = 0; r < yuvMatrix[c].length; r += blockDimension ) {
long[][] subBlock = new long[blockDimension][blockDimension];
//Iterate over the block in row-major order
for(int bc = 0; bc < blockDimension; bc++ ) {
for(int br = 0; br < blockDimension; br++ ) {
subBlock[bc][br]=yuvMatrix[c+bc][r+br];
}
}
blocks.add(subBlock);
}
}
return blocks;
}
That method doesn't look shorter but it is: discounting the preliminary check yours is missing there are only 8 actual lines of code compared to 13 in your code. That's not the point however. What's more important is that the logic is easier since there are only a few calculations involved (like c+bc).
You might think this is inefficient but it isn't: you're accessing each element only once and thus even though there are 4 nested loops the overall complexity is still O(n) with n being the size of the matrix.
Constructing the matrix back is equally easy. The major thing you need to take care of is the ordering of the blocks: if you create them in row-major order (blocks below each other are next to each other in the list) you need to recreate the matrix in the same way:
public static long[][] blocksToMatrix( List<long[][]> blocks, int width, int height ) {
long[][] yuvMatrix = new long[width][height];
int c = 0;
int r = 0;
for( long[][] block : blocks ) {
int blockWidth = block.length;
int blockHeight = block[0].length;
for( int bc = 0; bc < block.length; bc++ ) {
for( int br = 0; br < block[bc].length; br++ ) {
yuvMatrix[c + bc][r + br] = block[bc][br];
}
}
//calculate the next offset into the matrix
//The blocks where created in row-major order so we need to advance the offset in the same way
r += blockHeight;
if( r >= height ) {
r = 0;
c += blockWidth;
}
}
return yuvMatrix;
}
I'm trying to code that If the sign value of the slope changed from positive to negative, then that value indexing is my pressure peak of the sensor. and even through if it found the peak, there might be another peak because of the pressure is increasing.
How can I do that please? Thank you in advance.
for (int i =1;i<strNumbers.length -2;i++){ //strNumber is the array stream from sensor.
String y1;
y1 = strNumbers[i];
String y2;
y2 = strNumbers[i+1];
float y_2 = Float.parseFloat(y2);
float y_1 = Float.parseFloat(y1);
float delta_y = y_2 - y_1;
float mySlope = 0;
float delta_x;
delta_x = 1; //always difference is 1.
mySlope = (delta_y / delta_x);
activity.FirstPeak.setTextColor(Color.RED);
activity.FirstPeak.setText(String.valueOf(mySlope));
}
At each point in the loop, you need to compare two gradients - the gradient on the left, and the gradient on the right. If the left is positive, and the right is negative, then you have found a peak.
List<Integer> peaks = new ArrayList<>();
for (int i = 1; i < strNumbers.length - 1; i++)
{
float left = Float.parseFloat(strNumbers[i]) - Float.parseFloat(strNumbers[i - 1]);
float right = Float.parseFloat(strNumbers[i + 1]) - Float.parseFloat(strNumbers[i]);
if (left > 0 && right <= 0)
peaks.add(i);
}
Now you have an array, containing the indices of all peaks. You can find the absolute maximum like this:
float max = 0;
float maxIndex = -1;
for (Integer i : peaks)
{
float peak = Float.parseFloat(strNumbers[i]);
if (peak > max)
{
max = peak;
maxIndex = i;
}
}
And you can find the peak-to-peak distances like this:
int sum = 0;
for (int i = 0; i < peaks.size() - 1; i++)
sum += peaks.get(i + 1) - peaks.get(i);
float average = (float)sum / (peaks.size() - 1)
I have a sound file that I am practicing phase vocoding on. I have converted the bytes of the file into a double[] and manipulated the wave form of that file through a Fast Fourier Transform and inverse Fast Fourier Transform. The issue is now I need to convert the byte[] back into a double. Here are some useful code fragments:
How I converted the data in the first place:
/**
* Converts bytes from a TargetDataLine into a double[] allowing the information to be read.
* NOTE: One byte is lost in the conversion so don't expect the arrays to be the same length!
* #param bufferData The buffer read in from the target Data Line
* #return the double[] that the buffer has been converted into.
*/
private static double[] bytesToDoubleArray(byte[] bufferData){
final int bytesRecorded = bufferData.length;
final int bytesPerSample = getAudioFormat().getSampleSizeInBits()/8;
final double amplification = 100.0; // choose a number as you like
double[] micBufferData = new double[bytesRecorded - bytesPerSample + 1];
for (int index = 0, floatIndex = 0; index < bytesRecorded - bytesPerSample + 1; index += bytesPerSample, floatIndex++) {
double sample = 0;
for (int b = 0; b < bytesPerSample; b++) {
int v = bufferData[index + b];
if (b < bytesPerSample - 1 || bytesPerSample == 1) {
v &= 0xFF;
}
sample += v << (b * 8);
}
double sample32 = amplification * (sample / 32768.0);
micBufferData[floatIndex] = sample32;
}
return micBufferData;
}
and what I am doing to the data:
public static byte[] shift(byte[] data, int factor){
double[] audioData = bytesToDoubleArray(data);
audioData = Arrays.copyOf(audioData, roundToPowerOf2(audioData.length));
Complex[] transformed = FFT.fft(doubleToComplex(audioData));
transformed = shiftArray(transformed, 3);
Complex[] reverted = FFT.ifft(transformed);
for(int i = 0; i<reverted.length; i++){
audioData[i] = reverted[i].re();
}
return null;//How do I convert audioData[] back into a byte[]????
}
Any ideas on how to remedy this problem? Any solutions would be greatly appreciated. Also any Java libraries that already implement phase vocoding would be awesome as well.
Here it is, approximately. I'm sure I've screwed something up. scaleFactor would presumably be 327.68, to reverse the scaling above. The above code appears to be big endian. Whether you want fullNormalize is up to you.
public byte[] doubleArrayToByteArray(double[] input, int bytesPerSample, double scaleFactor, boolean fullNormalize, boolean bigEndian) {
byte[] result = new byte[input.length * bytesPerSample];
performNormalization(input, scaleFactor, fullNormalize);
for (int i = 0; i < input.length; i++) {
long sourceVal = (long)(input[i] * scaleFactor);
sourceVal = sourceVal >> 8 * (8 - bytesPerSample);
for (int j = 0; j < bytesPerSample; j++) {
int index = i * bytesPerSample;
if (bigEndian) {
index += (bytesPerSample - j);
}
else {
index += j;
}
result[index] = (byte) sourceVal;
sourceVal = sourceVal >> 8;
}
}
return result;
}
public void performNormalization(double[] input, double scaleFactor, boolean fullNormalize) {
double maxVal = 0.0;
for (int i = 0; i < input.length; i++) {
double val = Math.abs(input[i]) * scaleFactor;
if (val > maxVal) {
maxVal = val;
}
}
if (fullNormalize || maxVal > Long.MAX_VALUE) {
double normalizeFactor = (double)(Long.MAX_VALUE) / maxVal;
for (int i = 0; i < input.length; i++) {
input[i] *= normalizeFactor;
}
}
}
Updated: Realized that I needed to account for scaleFactor in normalization. And you'd not normally specify both a scaleFactor that was not 1.0 and fullNormalize = true.
It depends on what you mean by converting double[] to byte[]. Recall that typically double is 8 bytes length while byte is 1 byte long.
If you can convert your double somehow to an unsigned integer 0-255 (or signed -128 - 127) then you just need to cast each double to byte and assigned it to a new array, otherwise you have to use ByteBuffer:
int capacity = // calculate how much space you need for your byte array
ByteBuffer byteBuf = ByteBuffer(capacity);
for(double d : doubleArray) byteBuf.putDouble(d);
byte[] byteArray = byteBuf.array();
Effectively the above method did not convert anything but you're just interpreting the byte stream as a sequence of bytes rather than sequence of double
Just as you did when you converted from byte[] to double[], you will now need to convert back to byte[]. Just create a new byte[] array and manually copy the data from the double[] array:
private static byte[] doublesToByteArray(double[] a) {
int len = a.length;
byte[] b = new byte[len];
for (int i = 0; i < len; i++)
b[i] = (byte) a[i];
return b;
}
This of course assumes that the data in the double[] array can be stored in a byte[] array without further conversion / scaling. Otherwise you will need to take care of that when copying.
i read ecg byte array from file.now i want to detect QRS of read ecg byte.
how can i acheive this in java.
i get byte array from Lifegain defibrilator(an ecg device).i draw ecg on android from these bytes.now i want to detect QRS complex(term used for calculation of time and voltage of a wave of one heart beat).DATA=LeadData":"-284,-127,-122,17,-35,10,32,10,52,16,49,33,38,69,70,58,45,93,47,88,58,90,149,5,82,-12,-4,40,-34,29,-29,5,-4,-17,-13,-29,-13,-4,-9,-9,-10,-20,-15,-22,-32,-25,-23,-2,-15,-7,-13,-19,-17,-28,-27,-27,-33,-20,-16,-13,-20,-10,-22,-20,-19,-28,-15,-19,-22,-21,-9,-3,-6,-8,-6,-11,-8,-8,-5,-10,-5,-6,-9,-4,-6,3,20,3,14,7,11,10,5,11,5,10,2,10,13,14"
Regards,
shah
If the data you have is the data I think you have, you need to use one of the algorithms to detect your QRS complex.
There are a lot of algorithms out there to detect a QRS complex, one of the easiest is A Moving Average based Filtering System with its Application to Real-time QRS Detection by HC Chen and SW Chen (you can get it on http://www.cinc.org/archives/2003/pdf/585.pdf).
The stages are:
High Pass filtering
Low Pass filtering
Descision making stage
From the low pass picture you can notice that now we have the peaks we need to detect our QRS complex. The last stage is the decision stage making. In the article you have a formula to implement this.
We need to know when a QRS complex starts, so we need to set a threshold for this. The formula in this implementation is:
threshold = alpha * gamma * peak + (1 - alpha) * threshold
The peak is the local maximum in the window (we usually search the signal with a window of width 250), the threshold is the initial value (the first one can be the firstly found peak), alpha and gamma are randomly created, alpha is greater than 0 and smaller than 1 whereas gamma is 0.15 or 0.20. If the value of the current signal exceeds the threshold, a QRS complex is found.
Here is the source code in Java for low pass, high pass and decision making:
// High pass filter
// y1[n] = 1/M * Sum[m=0, M-1] x[n-m]
// y2[n] = x[n - (M+1)/2]
public static float[] highPass(int[] sig0, int nsamp) {
float[] highPass = new float[nsamp];
int M = 5; // M is recommended to be 5 or 7 according to the paper
float constant = (float) 1/M;
for(int i=0; i<sig0.length; i++) {
float y1 = 0;
float y2 = 0;
int y2_index = i-((M+1)/2);
if(y2_index < 0) {
y2_index = nsamp + y2_index;
}
y2 = sig0[y2_index];
float y1_sum = 0;
for(int j=i; j>i-M; j--) {
int x_index = i - (i-j);
if(x_index < 0) {
x_index = nsamp + x_index;
}
y1_sum += sig0[x_index];
}
y1 = constant * y1_sum;
highPass[i] = y2 - y1;
}
return highPass;
}
// Low pass filter; na n-to mesto zapiši kvadrat 30ih števil v oknu
public static float[] lowPass(float[] sig0, int nsamp) {
float[] lowPass = new float[nsamp];
for(int i=0; i<sig0.length; i++) {
float sum = 0;
if(i+30 < sig0.length) {
for(int j=i; j<i+30; j++) {
float current = sig0[j] * sig0[j];
sum += current;
}
}
else if(i+30 >= sig0.length) {
int over = i+30 - sig0.length;
for(int j=i; j<sig0.length; j++) {
float current = sig0[j] * sig0[j];
sum += current;
}
for(int j=0; j<over; j++) {
float current = sig0[j] * sig0[j];
sum += current;
}
}
lowPass[i] = sum;
}
return lowPass;
}
public static int[] QRS(float[] lowPass, int nsamp) {
int[] QRS = new int[nsamp];
double treshold = 0;
for(int i=0; i<200; i++) {
if(lowPass[i] > treshold) {
treshold = lowPass[i];
}
}
int frame = 250;
for(int i=0; i<lowPass.length; i+=frame) {
float max = 0;
int index = 0;
if(i + frame > lowPass.length) {
index = lowPass.length;
}
else {
index = i + frame;
}
for(int j=i; j<index; j++) {
if(lowPass[j] > max) max = lowPass[j];
}
boolean added = false;
for(int j=i; j<index; j++) {
if(lowPass[j] > treshold && !added) {
QRS[j] = 1;
added = true;
}
else {
QRS[j] = 0;
}
}
double gama = (Math.random() > 0.5) ? 0.15 : 0.20;
double alpha = 0.01 + (Math.random() * ((0.1 - 0.01)));
treshold = alpha * gama * max + (1 - alpha) * treshold;
}
return QRS;
}
Please follow the link below , i think they will help you.
http://www.cinc.org/archives/2008/pdf/0441.pdf
http://carg.site.uottawa.ca/doc/ELG6163GeoffreyGreen.pdf
http://www.eplimited.com/osea13.pdf
http://mirel.xmu.edu.cn/mirel/public/Teaching/QRSdetection.pdf
http://sourceforge.net/projects/ecgtoolkit-cs/files/ecgtoolkit-cs/ecgtoolkit-cs-2_2/
I want to add the diagonals in a square or rectangular matrix to emulate the process of adding the partial results in a multiplying algorithm.
Like this:
2412
x 3231
---------
2412
7236
4824
+ 7236
---------
7793172
I need to run this, step by step, to satisfy the requirements of an online judge program. I have already figured out how to get the partial results of the multiplications (the humbers 2412, 7236, 4824, 7236) and I have placed them on a square matrix.
I realized I can get the addition result of this matrix by considering square or rectangular like:
2 4 1 2
7 2 3 6
4 8 2 4
7 2 3 6
and get the result of the addition by adding each diagonal (starting with the upper right one) and taking into account the carry of the addition and using an auxiliary array that has the same number of digits as number_of_digits_in_operand_a + number_of_digits_in_operand_b (operand a being 2412 and operand b being 3231, in this case).
For example, the array result, on its rightmost position should be:
result[(digits_a+digits_b)-1] = partialResult[0][3];
next:
result[digits_a+digits_b]=(partialResult[0][2] + partialResult[1][3] + carry) %10;
newCarry = (partialResult[0][2] + partialResult[1][3] + carry) / 10;
Well, I'm stuck writing the double nested loop that's supposed to add these diagonals starting with the upper right one. Help. Please.
I ended up using this (don't ask why it converts a BigInteger to an ArrayList and viceversa, it's a bizarre homework requirement).
public static BigInteger simpleMultiply(BigInteger x, BigInteger y) throws IOException {
char [] longerNum;
char [] shorterNum;
ArrayList<Integer> multResult= new ArrayList<Integer>(2000);
if(x.compareTo(y)>=0){ // x is a longer/equal num
longerNum = x.toString().toCharArray();
shorterNum = y.toString().toCharArray();
}
else { //y is a longer num
longerNum = y.toString().toCharArray();
shorterNum = x.toString().toCharArray();
}
//shorter num equals the number of rows in partial result
// longer num + 1 equals the number of columns in partial result
int [][] partialResult = new int [shorterNum.length][longerNum.length+1];
int pastCarry=0;
int result=0;
int carry=0;
for (int sIndex=(shorterNum.length-1); sIndex>=0; sIndex--){
pastCarry=0;
for (int lIndex = (longerNum.length-1); lIndex>=0; lIndex--)
{
int sInt = Integer.parseInt(""+shorterNum[sIndex]+"");
int lInt = Integer.parseInt(""+longerNum[lIndex]+"");
int product = sInt*lInt;
if (lIndex==0){
result = (pastCarry+product)% 10;
carry = (pastCarry+product) / 10;
pastCarry = carry;
partialResult [sIndex][lIndex+1] = result; //one more column element in partialResult
partialResult[sIndex][lIndex] = carry;
}
else {
result = (pastCarry+product) % 10;
carry = (pastCarry+product) / 10;
pastCarry = carry;
partialResult [sIndex][lIndex+1] = result;//one more column element in partialResult
}
}
}
for (int i=0; i<partialResult.length;i++)
for (int j=0; j<partialResult[0].length;j++)
{
System.out.print(partialResult[i][j] + " ");
if (j==partialResult[0].length-1){System.out.println();}
}
int auxColumn=0;
int diagonalAcum=0;
//add diagonals
int copyDigit=0;
int carryDigit=0;
int lastCarry=0;
rowCycle:
for (int column=partialResult[0].length-1; column>=0; column--){
diagonalAcum=0; //carryDigit=0;
diagonalAcum+=carryDigit;
auxColumn=column;
for (int row=0; row<partialResult.length; row++){
if (auxColumn+1 ==partialResult[0].length){
diagonalAcum+=partialResult[row][auxColumn++];
copyDigit=diagonalAcum % 10;
carryDigit=diagonalAcum / 10;
multResult.add(copyDigit);
continue rowCycle;
}
diagonalAcum+=partialResult[row][auxColumn++];
} //end row cycle
copyDigit= diagonalAcum % 10;
carryDigit=diagonalAcum / 10;
multResult.add(copyDigit);
if(column==0){
lastCarry = carryDigit;
}
}
carryDigit=0; //reset
int diagonal2Acum=0;
// diagonal2Acum +=lastCarry;
int auxRow;
int diagCarry=0;
int rowLimit=partialResult.length-1;
int colLimit=partialResult[0].length-1;
int initialRow=1;
int colIndex=0;
for (int row=initialRow;row<=rowLimit;row++){
diagonal2Acum=0;
diagonal2Acum +=lastCarry;
lastCarry=0;
auxRow = row;
colIndex=0;
// partialResult[auxRow][]
while ((auxRow<=rowLimit) && (colIndex<=colLimit)){
diagonal2Acum+= partialResult[auxRow++][colIndex++];
}
if ((colIndex==0)&&(row==rowLimit)) {
copyDigit=(diagonal2Acum+carryDigit)%10;
carryDigit=(diagonal2Acum+carryDigit)/10;
multResult.add(copyDigit);
multResult.add(carryDigit);
}
else {
copyDigit=(diagonal2Acum+carryDigit)%10;
carryDigit=(diagonal2Acum+carryDigit)/10;
multResult.add(copyDigit);
}
} // end row for
StringBuilder appended = new StringBuilder();
for (int i=multResult.size()-1;i>=0;i--){
appended.append(multResult.get(i));
}
System.out.println("result is " + appended.toString());
BigInteger the_result1 = new BigInteger(appended.toString());
return the_result1;
}
Assume your partialResult dimensions are width and height you can add by the following two loops (see it here in action):
int digit = width + height - 1;
int carry = 0;
for (int d1 = width - 1; d1 >= 0; d1--) {
for (int r = 0; r < height && d1 + r < width; r++)
carry += partialResult[r][d1 + r];
result[--digit] = carry % 10;
carry /= 10;
}
for (int d2 = 1; d2 < height; d2++) {
for (int c = 0; c < width && d2 + c < height; c++)
carry += partialResult[d2 + c][c];
result[--digit] = carry % 10;
carry /= 10;
}
Note: Carry may be non-empty at the end meaning another digit before the first one in result.