Referring to a 2D array through a 1D array
-
This is what I would like to do: 1. Read in a WAV file 2. Break it up into segments of 256 samples each, with an overlap of 64 samples. 3. Refer to each segment as Segment[i] where "i" is the number of samples. I'm done with steps 1 and 2. This is the code block that I used to segment the samples: // *** CODE FOLLOWS *** // For the WAV file I'm working with, there are 17 such segments for(int i = 0; i < 17; i++) { for(int j = 0; j < 256; j++) { WaveSegment[i,j] = objWaveToSegment.Data[(i*64) + j]; } } /// *** END OF CODE *** In the above snippet, objWaveToSegment is an object of another class that reads in the WAV file and provides access to it's data. Now, how do I create 17 1*256 arrays called "Segment" such that when I refer to Segment[1] I get samples 0..255, Segment[2] is 64..319 etc. Thank you for your replies in advance :)
-
This is what I would like to do: 1. Read in a WAV file 2. Break it up into segments of 256 samples each, with an overlap of 64 samples. 3. Refer to each segment as Segment[i] where "i" is the number of samples. I'm done with steps 1 and 2. This is the code block that I used to segment the samples: // *** CODE FOLLOWS *** // For the WAV file I'm working with, there are 17 such segments for(int i = 0; i < 17; i++) { for(int j = 0; j < 256; j++) { WaveSegment[i,j] = objWaveToSegment.Data[(i*64) + j]; } } /// *** END OF CODE *** In the above snippet, objWaveToSegment is an object of another class that reads in the WAV file and provides access to it's data. Now, how do I create 17 1*256 arrays called "Segment" such that when I refer to Segment[1] I get samples 0..255, Segment[2] is 64..319 etc. Thank you for your replies in advance :)
Probably a too simple answer but I suppose this would do the job public WaveSegment[] GetWaveSegment(int index){ WaveSegment[] ws=new WaveSegment[256]; for (int arrIdx=0;arrIdx,256;arrIdx++0{ ws[arrIdx]=WaveSegment[index,arrIdx]; } return ws; } Maybe you could also investigate the Array.Copy static method
-
This is what I would like to do: 1. Read in a WAV file 2. Break it up into segments of 256 samples each, with an overlap of 64 samples. 3. Refer to each segment as Segment[i] where "i" is the number of samples. I'm done with steps 1 and 2. This is the code block that I used to segment the samples: // *** CODE FOLLOWS *** // For the WAV file I'm working with, there are 17 such segments for(int i = 0; i < 17; i++) { for(int j = 0; j < 256; j++) { WaveSegment[i,j] = objWaveToSegment.Data[(i*64) + j]; } } /// *** END OF CODE *** In the above snippet, objWaveToSegment is an object of another class that reads in the WAV file and provides access to it's data. Now, how do I create 17 1*256 arrays called "Segment" such that when I refer to Segment[1] I get samples 0..255, Segment[2] is 64..319 etc. Thank you for your replies in advance :)
Why not just use a jagged array, a.k.a. an array of arrays? When you get
Segment[0]
(note, all .NET languages use 0-based indexes, not 1-based indexes!) you get an array of 256 elements, whatever they are:WaveSegment[] segments = new WaveSegment[17];
for (int i=0; iYou can then access them in several ways, including:WaveSegment[] innerSegments = segments[0];
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK----- -
Probably a too simple answer but I suppose this would do the job public WaveSegment[] GetWaveSegment(int index){ WaveSegment[] ws=new WaveSegment[256]; for (int arrIdx=0;arrIdx,256;arrIdx++0{ ws[arrIdx]=WaveSegment[index,arrIdx]; } return ws; } Maybe you could also investigate the Array.Copy static method
In this example, is WaveSegment[] a separate class that I perform the segmentation in? Or is it a function? I apologize for my ignorance, but I'm really new to all this.
-
Why not just use a jagged array, a.k.a. an array of arrays? When you get
Segment[0]
(note, all .NET languages use 0-based indexes, not 1-based indexes!) you get an array of 256 elements, whatever they are:WaveSegment[] segments = new WaveSegment[17];
for (int i=0; iYou can then access them in several ways, including:WaveSegment[] innerSegments = segments[0];
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----Heath, thanks again for your help. Sorry for the index gaffe. I was aware of that, but was obviously not paying attention while typing. Could you please explain the following code: WaveSegment[] segments = new WaveSegment[17]; and WaveSegment[] innerSegments = segments[0]; What is WaveSegment[] here? A class, a function or a jagged array? I'm new to all this, and apologize for the dumb questions.
-
Heath, thanks again for your help. Sorry for the index gaffe. I was aware of that, but was obviously not paying attention while typing. Could you please explain the following code: WaveSegment[] segments = new WaveSegment[17]; and WaveSegment[] innerSegments = segments[0]; What is WaveSegment[] here? A class, a function or a jagged array? I'm new to all this, and apologize for the dumb questions.
I don't know - you used it in your code fragment so I thought I'd reuse it. Maybe I'm using wrong in your context, but the idea is the same.
WaveSegment
could be anything: a class, struct, enum, or even a delegate! All you're doing is making an array of an array of something (no, I didn't stutter! :)). The first line made an array ofWaveSegment
with 17 elements. In my code fragment, I then created a new array of 256 elements within each of those 17 elements. The second line above merely gets that 256-element array from the first array element from the array with 17 elements. See Arrays[^] in the C# Language Features reference on MSDN, or more specifically Jagged Arrays[^] for more information.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
I don't know - you used it in your code fragment so I thought I'd reuse it. Maybe I'm using wrong in your context, but the idea is the same.
WaveSegment
could be anything: a class, struct, enum, or even a delegate! All you're doing is making an array of an array of something (no, I didn't stutter! :)). The first line made an array ofWaveSegment
with 17 elements. In my code fragment, I then created a new array of 256 elements within each of those 17 elements. The second line above merely gets that 256-element array from the first array element from the array with 17 elements. See Arrays[^] in the C# Language Features reference on MSDN, or more specifically Jagged Arrays[^] for more information.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
Heath, thanks for your detailed replies! Ok, here's what I've done, incorporating your suggestions and that of my anonymous helper. public class Segmentation { private static void Main() { Random R = new Random(); int[] inArray = new int[1280]; for(int i = 0; i < 1280; i++) inArray[i] = i; int[][] arrSegmentArray = new int[17][]; for(int i = 0; i < 17; i++) { arrSegmentArray[i] = new int[256]; Array.Copy(inArray, 64*i, arrSegmentArray[i], 0, 256); Console.WriteLine(arrSegmentArray[i].ToString()); Console.ReadLine(); } } } However, where I expect arrSegmentArray[i] to be printed as a 256 element array, all I see is System.Int32[]; What do I need to fix here?
-
I don't know - you used it in your code fragment so I thought I'd reuse it. Maybe I'm using wrong in your context, but the idea is the same.
WaveSegment
could be anything: a class, struct, enum, or even a delegate! All you're doing is making an array of an array of something (no, I didn't stutter! :)). The first line made an array ofWaveSegment
with 17 elements. In my code fragment, I then created a new array of 256 elements within each of those 17 elements. The second line above merely gets that 256-element array from the first array element from the array with 17 elements. See Arrays[^] in the C# Language Features reference on MSDN, or more specifically Jagged Arrays[^] for more information.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
Heath, thanks for your detailed replies! Ok, here's what I've done, incorporating your suggestions and that of my anonymous helper. public class Segmentation { private static void Main() { Random R = new Random(); int[] inArray = new int[1280]; for(int i = 0; i < 1280; i++) inArray[i] = i; int[][] arrSegmentArray = new int[17][]; for(int i = 0; i < 17; i++) { arrSegmentArray[i] = new int[256]; Array.Copy(inArray, 64*i, arrSegmentArray[i], 0, 256); Console.WriteLine(arrSegmentArray[i].ToString()); Console.ReadLine(); } } } However, where I expect arrSegmentArray[i] to be printed as a 256 element array, all I see is System.Int32[]; What do I need to fix here? :(
-
I don't know - you used it in your code fragment so I thought I'd reuse it. Maybe I'm using wrong in your context, but the idea is the same.
WaveSegment
could be anything: a class, struct, enum, or even a delegate! All you're doing is making an array of an array of something (no, I didn't stutter! :)). The first line made an array ofWaveSegment
with 17 elements. In my code fragment, I then created a new array of 256 elements within each of those 17 elements. The second line above merely gets that 256-element array from the first array element from the array with 17 elements. See Arrays[^] in the C# Language Features reference on MSDN, or more specifically Jagged Arrays[^] for more information.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
Solved it. Thanks for your help!
-
Heath, thanks for your detailed replies! Ok, here's what I've done, incorporating your suggestions and that of my anonymous helper. public class Segmentation { private static void Main() { Random R = new Random(); int[] inArray = new int[1280]; for(int i = 0; i < 1280; i++) inArray[i] = i; int[][] arrSegmentArray = new int[17][]; for(int i = 0; i < 17; i++) { arrSegmentArray[i] = new int[256]; Array.Copy(inArray, 64*i, arrSegmentArray[i], 0, 256); Console.WriteLine(arrSegmentArray[i].ToString()); Console.ReadLine(); } } } However, where I expect arrSegmentArray[i] to be printed as a 256 element array, all I see is System.Int32[]; What do I need to fix here? :(
Glad you solved it, but just for future benefit, the line
Console.WriteLine(arrSegmentArray[i].ToString());
is callingToString
on aInt32[]
array. The default implementation ofArray.ToString()
(inherited fromObject.ToString
) is to simply print the namespace-qualified class name.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
Glad you solved it, but just for future benefit, the line
Console.WriteLine(arrSegmentArray[i].ToString());
is callingToString
on aInt32[]
array. The default implementation ofArray.ToString()
(inherited fromObject.ToString
) is to simply print the namespace-qualified class name.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
What I did was this: for (int i =0; i < 17; i++) { for (int j=0; j < 256; j++) { Console.Write(arrSegmentArray[i][j].ToString()+" "); } Console.WriteLine(); Console.ReadLine(); } Is that alright?
-
What I did was this: for (int i =0; i < 17; i++) { for (int j=0; j < 256; j++) { Console.Write(arrSegmentArray[i][j].ToString()+" "); } Console.WriteLine(); Console.ReadLine(); } Is that alright?
Sure, why not?! :) It just depends on your requirements and implementation.
-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----