I am using an cellphone application to record cellphone gyroscope signal. I put the sampling rate to "fastest" which means the highest sampling rate the cellphone is able to do. What is strange is that when I try to plot the signal it is stair like "broken" (I dont know what to call a signal like this if anyone can help) What I dont understand is why is it like this? is it because of the resolution? or because the phone can't do a sampling rate say "500Hz" so it samples say "100Hz" and it duplicates each value 5 times so it gets us under the illusion that it was 500 Hz? anyone have some kind of reasoning for this? Also I would like to ask whether there is a way to interpolate or filter this kind of signal?
There are multiple possibilities.
One, as you said, cellphone may not be capable of 500Hz so it duplicates values. Did you verify whether cellphone is capable of it?
Two, gyroscope output itself may be duplicated. That is even if cellphone programs 500Hz, gyroscope may not be capable of it. Did you check data sheet of gyroscope how it reutrns values?
Three, Plotting tool will plot like this stair-case style. But that possibility may be ruled out in this case by looking at the plot and sample resolution.
So my advice would be to first check data sheet of gyroscope. It may not be able to sample at 500Hz, so it samples at lower rate and holds the value till next sample. This will result in such a kind of plot.
Your application that is triggering the internal HW of the phone System on Chip (via the OS), is triggering the sampling at lower rates (even though you set it to max)
There are also limitations of the sensor refresh rates. For ex: very comonly in today's cell phones we can get a temperature measurement at a maximum rate of 1ms (in high end phones). So, there are limitations of the application usage on the Hardware usage and also of the sensor itslef. In such cases what you see is a "sample and hold" till new value is available. Hence the stair like waveform
This appears to be either representing the lower sampled data using a zero-order hold process (which is what "sample and hold" would be referred to), or could simply be an artifact of the plotting tool (without seeing the source data). I assume the former since the OP mentioned 500 and 100 Hz sampling; so the data is sampled at 100 Hz and each value is held to provide a 500 Hz sampled signal- this is cheap and easy interpolation.
To answer the OP's question regarding interpolation or filtering: To clarify, I will refer to "original" data as the 100 Hz sampled signal and "resampled" or "interpolated" data as the 500 Hz sampled signal shown in the OP's plot. The data has already been interpolated using a zero-order hold (hold each original value for 4 more samples) which is very simple but doing this introduces "droop" in the frequency spectrum where the amplitude rolls off over frequency. The droop is really only an issue if the data has higher frequency content relative to the original sampling rate, so this may be of no consequence for the OP's application but something to be aware of. This is also very efficient and easy to do so the droop is often accepted or subsequently compensation for in such interpolation approaches.
However to understand how to do a better interpolation, below describes a typical zero-insert and filter interpolation approach that can be performed on this data. First, since there is no information added in the additional samples provided, and they introduce frequency droop as I described, select every 5th sample as the starting data ("original data") to the process described below:
General Interpolation Process, Interpolate by I
Step 1. Insert I-1 zeros in between each of the data samples
This will upsample the data to the higher rate $If_s$. Inserting zeros preserves the original spectrum with no distortion in band, but will replicate the spectrum $I-1$ times within the new frequency span from $f=0$ to $f=If_s$ where $f_s$ is the original sampling rate and $If_s$ is the new sampling rate multiplied by the factor $I$.
Step 2. Filter out the images
To complete the interpolation, we just need to remove all the images that were added within the spectrum. This is done with an interpolation filter. Since the original spectrum is not modified in the original bandwidth in any way, then perfect interpolation would be achieved with an interpolation filter that passes the passband with no distortion and completely reject the images. Perfect is not achievable but presents the design goals of the filter balanced with the acceptable distortion.
An excellent choice for image rejection filters are linear phase multiband FIR filters readily provided by the least-squares algorithm in MATLAB/Octave and Python Scipy. A demonstration of this using a 21 tap linear phase multi-band filter with Octave follows (increasing the N provides for more rejection at the expense of filter complexity):
N = 21; coeff = firls(N, [0 .02 .18 .22 .38 .42 ]*2,[1 1 0 0 0 0]); out = filter(coeff,1,sig);
The frequency response of the filter is shown overlaid with the zero-insert spectrum to show how the filter rejection is optimized at the image locations. Using the multi-band filter approach maximizes the rejection where it is needed for a given filter complexity (number of taps).
Resulting in the following spectrum:
An overlay of the two spectrums in vicinity of the passband signal is shown below:
A zoom in of the time domain plot shows specifically how the interpolated samples are placed in comparison to the zero-order hold (sample and hold). Note the interpolated samples have been offset by the delay of the interpolation filter.