There is no in built method for finding the sampling frequency of the Arduino mega.
The Sampling frequency depends on a lot of factors
- Clock frequency/Machine cycle of the mega
- Time for serial communication interfaces
- Time taken by other instructions in the loop.
Hence, the sampling frequency is defined in a microcontroller.
It is possible to find the time between two subsequent readings , thus calculating the frequency between two samples (sampling frequency)
This can be done by storing the time of the microcontroller before reading the analog value from the sensor and subtracting this time from the time the next sample is read.
This would give you
Ts(time between two samples)
and
Fs = 1/Ts
However, it is possible to achieve this task without having to use the sampling frequency in a user MATLAB function if you’re using simulink, as shown in this documentation link.