Short Time Fourier Transform in C# - c#

I'm writing the following code that implements the stft (http://en.wikipedia.org/wiki/Short-time_Fourier_transform) through the Exocortex library (http://www.exocortex.org/dsp/), where "window" is a 512 Hamming window, "left" is the left channel from an audio stream passed into a double array, "frames" is the number of frames I split the audio file into (say around 600) and the "offset" variable performs the overlap.
I've also added the ability of zero padding in the Exocortex FFT class:
if( data.Length < length ) {
for (int k = data.Length; k < length; k++)
{
data[k].Re = 0;
data[k].Im = 0;
}
The problem is that for frames=600 this code runs for about 7000msec! Am I missing something here?
Exocortex.DSP.ComplexF[] x_frame = new Exocortex.DSP.ComplexF[2048];
int offset = 0;
for (int m = 0; m < frames-1; m++)
{
for (int i = 0; i < 512; i++)
{
x_frame[i].Re = (float)(left[i + offset] * window[i]);
x_frame[i].Im = 0;
}
Exocortex.DSP.Fourier.FFT(x_frame, 2048, FourierDirection.Forward);
offset += 256;
}

Related

Get all pixel data from Bitmap object

I want to fill up a double[400] array with pixel data as seen in the below code.
I have been searching for the answer and I still haven't found one. My attempt is:
Bitmap bm = (Bitmap) Image.FromFile(fileName);
numberVisual.Image = bm;
long overallIteration = 0;
for (long i = 0; i <= 20/*bm.Width*/; ++i)
{
for (long h = 0; h <= 20/*bm.Height*/; ++h)
{
input[/*(i * h)*/overallIteration] =
bm.GetPixel((int)h, (int)i) != Color.White ? 0 : 1;
++overallIteration;
}
}
It gives off an exception on
bm.GetPixel System.ArgumentOutOfRangeException: Parameter must be
positive and < Width.
All the bitmaps I am using are 20x20px.
Please check the height and the width of the image you are loading, if your image is 20x20, you have to iterate from 0 to 19 and not from 0 to 20
Right code
for (long i = 0; i < 20; ++i)
{
for (long h = 0; h < 20; ++h)
{
Wrong code
for (long i = 0; I <= 20; ++i)
{
for (long h = 0; h <= 20; ++h)
{

C#: Out of memory Exception

I have written a console application
Int64 sum = 0;
int T = Convert.ToInt32(Console.ReadLine());
Int64[] input = new Int64[T];
for (int i = 0; i < T; i++)
{
input[i] = Convert.ToInt32(Console.ReadLine());
}
for (int i = 0; i < T; i++)
{
int[,] Matrix = new int[input[i], input[i]];
sum = 0;
for (int j = 0; j < input[i]; j++)
{
for (int k = 0; k < input[i]; k++)
{
Matrix[j, k] = Math.Abs(j - k);
sum += Matrix[j, k];
}
}
Console.WriteLine(sum);
}
When I gave input as
2
1
999999
It gave Out of memory exception. Can you please help.
Look at what you are allocating:
input[] is allocated as 2 elements (16 bytes) - no worries
But then you enter values: 1 and 999999 and in the first iteration of the loop attempt to allocate
Matrix[1,1] = 4 bytes - again no worries,
but the second time round you try to allocate
Matrix[999999, 999999]
which is 4 * 10e12 bytes and certainly beyond the capacity of your computer even with swap space on the disk.
I suspect that this is not what you really want to allocate (you'd never be able to fill or manipulate that many elements anyway...)
If you are merely trying to do the calculations as per your original code, there is not need to allocate or use the array, as you only ever store one value and immediately use that value and then never again.
Int64 sum = 0;
int T = Convert.ToInt32(Console.ReadLine());
Int64[] input = new Int64[T];
for (int i = 0; i < T; i++)
input[i] = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < T; i++)
{
// int[,] Matrix = new int[input[i], input[i]];
sum = 0;
for (int j = 0; j < input[i]; j++)
for (int k = 0; k < input[i]; k++)
{
//Matrix[j, k] = Math.Abs(j - k);
//sum += Matrix[j, k];
sum += Math.Abs(j - k);
}
Console.WriteLine(sum);
}
But now beware - a trillion sums is going to take forever to calculate - it won't bomb out, but you might like to take a vacation, get married and have kids before you can expect a result.
Of course instead of doing the full squared set of calculations, you can calculate the sum thus:
for (int i = 0; i < T; i++)
{
sum = 0;
for (int j = 1, term = 0; j < input[i]; j++)
{
term += j;
sum += term * 2;
}
Console.WriteLine(sum);
}
So now the calculation is O(n) instead of O(n^2)
And if you need to know what the value in Matrix[x,y] would have been, you can calculate it by the simple expression Math.Abs(x - y) thus there is no need to store that value.

convert 3 dimensional array to a 2 dimensional array

I have this byte array:
byte[,,] somedata = new byte[100,150,3];
~somedata is now populated by an external input.
I want to use the values in just this:
byte[,] useThisDataOnly = new byte[100,150];
Presently, I use this:
foreach (int x=0,x< 100; x++)
{
foreach (int y=0,y< 150; y++)
{
useThisDataOnly [x,y] = somedata[x,y,0];
}
}
Is there a quicker way to do this at all?
Also, I am going to flatten this useThisDataOnly (as I heard it is quicker to manipulate with).
Any suggestions?
The difficulty you have with optimising this is that the bytes that you want to leave out are in the most "rapidly moving" index.
What I mean by that is: If you assign consecutive numbers to each array element like so:
byte[,,] source = new byte[100, 150, 3];
for (int i = 0, n = 0; i < 100; ++i)
for (int j = 0; j < 150; ++j)
for (int k = 0; k < 3; ++k, ++n)
source[i, j, k] = unchecked ((byte)n);
If you printed out the contents in flattened order, you'd get 0..255 repeating.
Then you exclude the final dimension:
for (int i = 0; i < 100; ++i)
for (int j = 0; j < 150; ++j)
dest[i, j] = source[i, j, 0];
If you print the result out in flattened order, you'll get:
0, 3, 6, 9, 12, 15, ...
So you can see that in order to transform the input to the output you need to take every third byte, and unfortunately there is no quick way to do that.
However, if you were able to ask for the array to be created with the index to be omitted first:
byte[,,] source = new byte[3, 100, 150];
then you would be able to use Buffer.BlockCopy().
I guess this isn't an option - if not, the hand-coded loop is probably the quickest you'll be able to do it without resorting to unsafe code.
Unsafe code
Here's how you can do it using unsafe code and pointers. This might not even make it faster, so before choosing to do it like this you need to (a) be sure that doing the simple way is really too slow and (b) do some careful timings of a release build using Stopwatch to ensure that it really is worth using unsafe code.
Also be aware of the drawbacks of using unsafe code! Unsafe code requires elevated permissions and can cause your program to crash with low-level C++-style wild pointer errors!
using System;
using System.Diagnostics;
namespace Demo
{
internal static class Program
{
static void Main()
{
byte[,,] source = new byte[100, 150, 3];
for (int i = 0, n = 0; i < 100; ++i)
for (int j = 0; j < 150; ++j)
for (int k = 0; k < 3; ++k, ++n)
source[i, j, k] = unchecked ((byte)n);
byte[,] dest1 = new byte[100, 150];
byte[,] dest2 = new byte[100, 150];
for (int i = 0; i < 100; ++i)
for (int j = 0; j < 150; ++j)
dest1[i, j] = source[i, j, 0];
unsafe
{
fixed (byte* sp = source)
fixed (byte* dp = dest2)
{
byte* q = dp;
byte* p = sp;
for (int i = 0; i < 100*150; ++i)
{
*q++ = *p;
p += 3;
}
}
}
for (int i = 0; i < 100; ++i)
for (int j = 0; j < 150; ++j)
Trace.Assert(dest1[i, j] == dest2[i, j], "Arrays should be identical");
}
}
}
I personally don't think that it will be too slow using the simple, safe loop anyway, but now at least you have some code to try out.

Best/Fastest Way To Change/Access Elements of a Matrix

I'm quite new to C# and I'm having difficult with our arrays, arrays of arrays, jagged arrays, matrixes and stuff. It's quite different from the C++ , since I can't get a reference (unless I use unsafe code) to a row of a matrix, using pointers and stuff.
Anyway, here's the problem: I have a struct/class called "Image" that cointains 1024 columns and 768 lines. For each line/column theres a 'pixel' struct/class that contains 3 bytes. I'd like to get/set pixels in random places of the matrix as fast as possible.
Let's pretend I have a matrix with 25 pixels. That is 5 rows and 5 columns, something like this:
A B C D E
F G H I J
K L M N O
P Q R S T
U V X W Y
And I need to compare M to H and R. Then M to L and N. Then I need to 'sum' G+H+I+L+M+N+Q+R+S.
How can I do that?
Possibilities:
1) Create something like pixel[5][5] (that's a jagged array, right?), which will be slow whenever I try to compare elements on different columns, right?
2) Create something like pixel[25] , which won't be as easy to code/ready because I'll need to do some (simple) math each and everything I want to access a element
3) Create something like pixe[5,5] (that's a multi-dimensional array, right?)... But I don't know how that will be translated to actual memory... If it's going to be a single block of memory, like the pixe[25], or what...
Since I intend to do this operations ('random' sums/comparison of elements that are in different rows/columns) tens of thousands of times per image. And I have 1000+ imagens. Code optimization is a must... Sadly I'm not sure which structure / classe I should use.
TL;DR: Whats the FASTEST and whats the EASIEST (coding wise) way of getting/setting elements in random positions of a (fixed size) matrix?
edit: I do not want to compare C++ to C#. I'm just saying I AM NEW TO C# and I'd like to find the best way to accomplish this, using C#. Please don't tell me to go back to C++.
I have worked on pixel based image processing in C#. I found that pattern #2 in your list is fastest. For speed, you must forget about accessing pixels via some kind of nice abstract interface. The pixel processing routines must explicitly deal with the width and height of the image. This makes for crappy code in general, but unless Microsoft improves the C# compiler, we are stuck with this approach.
If your for loops start at index 0 of an array, and end at array.Length - 1, the compiler will optimize out the array index bounds testing. This is nice, but typically you have to use more than one pixel at a time while processing.
I just finished testing, heres the result:
SD Array Test1: 00:00:00.9388379
SD Array Test2: 00:00:00.4117926
MD Array Test1: 00:00:01.4977765
MD Array Test2: 00:00:00.8950093
Jagged Array Test1: 00:00:03.6850013
Jagged Array Test2: 00:00:00.5036041
Conclusion: Single dimensional array is the way to go... Sadly we lose in readability.
And heres the code:
int[] myArray = new int[10000 * 10000];
for (int i = 0; i < 10000; i++)
{
for (int j = 0; j < 10000; j++)
{
myArray[(i*10000)+j] = i+j;
}
}
Stopwatch sw = new Stopwatch();
int sum = 0;
sw.Start();
for (int i = 0; i < 10000; i++)
{
for (int j = 0; j < 10000; j++)
{
sum += myArray[(j * 10000) + i];
}
}
sw.Stop();
Console.WriteLine("SD Array Test1: " + sw.Elapsed.ToString());
sum=0;
sw.Restart();
for (int i = 0; i < 10000; i++)
{
for (int j = 0; j < 10000; j++)
{
sum += myArray[(i * 10000) + j];
}
}
sw.Stop();
Console.WriteLine("SD Array Test2: " + sw.Elapsed.ToString());
myArray = null;
int[,] MDA = new int[10000, 10000];
for (int i = 0; i < 10000; i++)
{
for (int j = 0; j < 10000; j++)
{
MDA[i, j] = i + j;
}
}
sum = 0;
sw.Restart();
for (int i = 0; i < 10000; i++)
{
for (int j = 0; j < 10000; j++)
{
sum += MDA[j, i];
}
}
sw.Stop();
Console.WriteLine("MD Array Test1: " + sw.Elapsed.ToString());
sw.Restart();
for (int i = 0; i < 10000; i++)
{
for (int j = 0; j < 10000; j++)
{
sum += MDA[i, j];
}
}
sw.Stop();
Console.WriteLine("MD Array Test2: " + sw.Elapsed.ToString());
MDA = null;
int[][] JA = new int[10000][];
for (int i = 0; i < 10000; i++)
{
JA[i] = new int[10000];
}
for (int i = 0; i < 10000; i++)
{
for (int j = 0; j < 10000; j++)
{
JA[i][j] = i + j;
}
}
sum = 0;
sw.Restart();
for (int i = 0; i < 10000; i++)
{
for (int j = 0; j < 10000; j++)
{
sum += JA[j][i];
}
}
sw.Stop();
Console.WriteLine("Jagged Array Test1: " + sw.Elapsed.ToString());
sw.Restart();
for (int i = 0; i < 10000; i++)
{
for (int j = 0; j < 10000; j++)
{
sum += JA[i][j];
}
}
sw.Stop();
Console.WriteLine("Jagged Array Test2: " + sw.Elapsed.ToString());
MDA = null;
Console.ReadKey();

Algorithm to vertically flip a bitmap in a byte array

I am writing a class for printing bitmaps to a portable bluetooth printer in Android via Mono For Android. My class is used to obtain the pixel data from the stream so that is can be sent to the printer in the correct format. Right now the class is simple, it just reads the height, width, and bits per pixel.
Using the offset it reads and returns the pixel data to the printer. Right now I am just working with 1 bit per pixel black and white images. The bitmaps I am working with are in Windows format.
Here is the original image:
Here is the result of printing, the first image is without any transformation. And the second one is the result of modifying the BitArray with the following code:
BitArray bits = new BitArray(returnBytes);
BitArray flippedBits = new BitArray(bits);
for (int i = 0, j = bits.Length - 1; i < bits.Length; i++, j--)
{
flippedBits[i] = bits[j];
}
My Question is:
How do I flip the image vertically when I am working with a byte array. I am having trouble finding the algorithm for doing this, all examples seem to suggest using established graphics libraries which I cannot use.
Edit:
My Bitmap is saved in a 1 dimensional array, with the first rows bytes, then the second, third, etc.
You need to do something like this:
BitArray bits = new BitArray(returnBytes);
BitArray flippedBits = new BitArray(bits);
for (int i = 0; i < bits.Length; i += width) {
for (int j = 0, k = width - 1; j < width; ++j, --k) {
flippedBits[i + j] = bits[i + k];
}
}
If you need to mirror picture upside-down, use this code:
BitArray bits = new BitArray(returnBytes);
BitArray flippedBits = new BitArray(bits);
for (int i = 0, j = bits.Length - width; i < bits.Length; i += width, j -= width) {
for (int k = 0; k < width; ++k) {
flippedBits[i + k] = bits[j + k];
}
}
For the format with width*height bits in row order, you just need to view the bit array as a two-dimensional array.
for(int row = 0; row < height; ++row) {
for(int column = 0; column < width; ++column) {
flippedBits[row*width + column] = bits[row*width + (width-1 - column)];
}
}
It would be a bit more complicated if there were more than one bit per pixel.
You need to use two loops, the first to iterate over all the rows and the second to iterate the pixels inside each row.
for (int y = 0; y < height; y++)
{
int row_start = (width/8) * y;
int flipped_row = (width/8) * (height-1 - y);
for (int x = 0; x < width/8; x++)
{
flippedBits[flipped_row+x] = bits[row_start+x];
}
}

Categories