Shifting values

Hi everyone, I'm using ESP32, and I've issue with my output signal. In some place on my signal, the top of the signal is shifted at the bottom or the bottom at the top as on the attached file.

As I'm printing samples[n], when changing the type of samples to uint32_t or int32_t, I've an insufficient memoryGuru Meditation Error

When using uint16_t all the negative values are shifted to the top and some top value are shifted to the bottom. I don't want the bottom and the top of the signal to be shifted. How can I solve this issue, you can see my code below.

Thanks in advance.

#include <SPI.h>    
#define SS 5   

int16_t* samples;
uint32_t n = 0;
int count = 0;
unsigned long start_time;
unsigned long end_time;
const unsigned long event = 10000;
const uint32_t max_samples=50000;

void setup() {
  Serial.begin(115200);                     /* initialization of serial communication */
  SPI.begin();                            /* initialization of SPI port */
  SPI.setDataMode(SPI_MODE0);             /* configuration of SPI communication in mode 0 */
  SPI.setClockDivider(SPI_CLOCK_DIV64);   /* configuration of clock at 1MHz */
  pinMode(SS, OUTPUT);
  Serial.println("Start");
  
  samples = (int16_t*)malloc(sizeof(int16_t)*max_samples);
  if(samples == NULL){
      Serial.print("insufficient memory");
    }
    else{
      Serial.print("malloc ok.");   
    }
}

void loop() {
  
  start_time = millis();
  for (n=0; n<max_samples; n++)
  {
    samples[n] = Sound();
  }
  end_time = millis();
//  Serial.print("Dauer [ms]: ");
//  Serial.print((end_time-start_time));
//  Serial.print(" ||| ");
//  Serial.print("Beats: ");
//  Serial.print(n);
//  Serial.println(" ||| ");
  for (n = 0; n < max_samples; n++)
  {
    Serial.print(samples[n]);
    //Serial.print(",-32768,32767");
    Serial.println();
  }
}

int16_t Sound(void) {
  digitalWrite(SS, LOW);                                        //activate chip select
  int sound = SPI.transfer(0) | (SPI.transfer(0) << 8);         //reconstruct 12-bit data
  digitalWrite(SS, HIGH);                                       //deactivate chip select
  return sound;
}

the u in uint16_t means unsigned ➜ there is no negative value

why do you bother with malloc() ? just create an array for your samples as a global variables

const uint32_t max_samples = 10000;
int16_t samples[max_samples];

what are you acquiring from?

  int sound = SPI.transfer(0) | (SPI.transfer(0) << 8);         //reconstruct 12-bit data

are you sure the 12 bit reconstruction is correct ?

PS: an int on your ESP32 will be 4 bytes and your Sound() function return an int16_t. May be you want to use an int16_t as well directly

dbl posting
https://forum.arduino.cc/t/deviations-in-output-signals/953361

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to [url=https://forum.arduino.cc/index.php?topic=710766.0]Learn How To Use The Forum[/url].

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.