Array, how to cut section out of array

Hello

I have some MATLAB code that I want to implement in Arduino. It involves searching through a large array for a trigger, and then cutting a portion from the array. Is is possible to do this in some short syntax? In MATLAB it looks something like array_section = large_array(i:i+64) which would cut the whole portion from i to i+64 into the new variable.

I just cannot find info on how to do this shorthand.

Any tips will be appreciated.

Thanks!

C

No, you cannot. If the array is constructed you can't change the size.

Btw, you are asking a X-Y problem

Are you asking how to copy part of an array to a whole new array? or like crop all the extra data out from the same array?

yes, X-Y problem indeed

Yeay, the statement he post is to copy a part to a new array. But you can use ofcourse the same methode to crop the array.

But the Arduino is a micro controller, not a computer. Memory is limited and array sizes need to be known at compiler time. So thing like "large arrays" and "cutting a part" sound like the wrong task for the Arduino. I don't say you can't do what you want. Maybe you can but you didn't tell use what you want. But the way you think you need to take is wrong.

septillion:

Memory is limited and array sizes need to be known at compiler time. So thing like “large arrays” and “cutting a part” sound like the wrong task for the Arduino. I don’t say you can’t do what you want. Maybe you can but you didn’t tell use what you want. But the way you think you need to take is wrong.

not completely true, Arduino can use dynamic memory and/or local variables. However I agree with septilion to use dynamic memory as minimal as possible as it can fragment heap.

The simpest way might involve a function:

int findTrigger(int *ar, int begin, int end)
{
  for (int idx = begin; idx < end; idx++)
  {
    if (ar[idx] == TRIGGER) return idx;
  }
  return -1;  // not found
}

Hi guys,

Thanks for the replies. I thought I was very clear on exactly what I wanted to do, but apparently not. :confused:

I do in fact want to copy a part of an array to a new array. I am sampling something as fast as possible, and then storing it in a array. Once I have finished sampling, I go through the array to find the event I am looking for. then I want to copy only that specific portion to a smaller new array for further analysis.

I can do it with a for loop, but just wanted to know if there is a short way of doing it. I am still learning the limitations of microcontrollers so thanks for the feedback.

Have a good day!

C

Rob, thanks for the code snippet. This will help me.

if (ar[idx] == TRIGGER) return idx; was the type of thing that I was not aware of. Thanks!

Have a good day.

C

@Rob, I know but if you start messing around with that before you know about some micro controller memory organization you get kicked in the back. Or in Dutch "kom je van een koude kermis thuis" (literally "you come home from a cold fair")

@theknack, if you already sampled it you can just use that array and store the start and end point inside the array of the piece you want and use that.

But that if statement is just a very basic if statment...

@septilion

you are right :slight_smile:

I do in fact want to copy a part of an array to a new array. I am sampling something as fast as possible, and then storing it in a array. Once I have finished sampling, I go through the array to find the event I am looking for. then I want to copy only that specific portion to a smaller new array for further analysis.

You haven't infinite RAM.

May I suggest searching for the event while sampling? What sort of event is it?

What model of Arduino are you using? Perhaps you could upgrade to a different model of Arduino.

There is no “shorthand” code to replicate this matlab functionality on the arduino, nor in the C/C++ language.

Nevertheless, it is not difficult ( apart from memory size restrictions ). If you need a copy of part of any array, and you have a fixed size required for it:

#define DATASIZE 100
#define CHUNKSIZE 10

int data[DATASIZE] ;
int chunk[CHUNKSIZE] ;
int match_int = 7 ;

bool found=false ;

for ( int i=0 ; i<DATASIZE ; i++ )
{
    if ( data[i] == match_int )
    {   
        int ncopy=CHUNKSIZE ;
        if ( i >= (DATASIZE-CHUNKSIZE) ) ncopy = DATASIZE-i ;
        memcpy( chunk, data, ncopy*sizeof(int) );
        found=true ;
        break ;
    }
}

@odometer, I am sampling a shockwave, so I do a basic level detection from the raw data continuously. As soon as I get a trigger I go into a separate loop to sample 2014 bits without doing any other operations. this way I know the sample rate will be as fast as possible and consistent. I cannot do all the math I am doing in real time as it will slow down the sampling (windowing, FFT, smoothing, averaging, differentiate etc.) I then cut out the shockwave and continue with the math. I am using the atmega, but planning on going over to the 0 as I need this to be as low power as possible, and thinking there are more options to switch things off that I don't need.

@ michinyon, thanks, this is the type of thing that I don't know, the last time I used micros was at university many ears ago, I am very rusty and don't "think" in micro terms anymore. Appreciate this, it will make my code much less repetitive.

As soon as I get a trigger I go into a separate loop to sample 2014 bits without doing any other operations.

How are you storing the 2014 bits ? Not in an array with 2014 elements I hope.

before upgrading to a zero, what are your speed and data requirements? When you say sampling, do you mean ADC sampling or simple 1's and 0's from digitalRead()?

Because either way, you can significantly improve those speeds on the ATmega328p (Arduino UNO) with just a few tweaks in your code.