How do you calculate the sampling rate of a time varying signal? - matlab

I'm trying to apply a filter method (Butterworth) in MATLAB to remove a static acceleration (gravity).
The problem here is the sampling rate seems to be varying. As far as I know, the sampling rate is defined as the number of samples obtained in one second (samples per second), thus fs = 1/T. T is not fixed, and it was varying in my file:
The sampling times were as follows. The fraction components represent ms.
16:25:50.032
16:25:50.192
16:25:50.352
16:25:50.512
16:25:50.671
16:25:50.832
16:25:50.991
16:25:51.151
16:25:51.312
16:25:51.472
16:25:51.632
The value of T is 100ms but here we can see that T varies between 159 and 161. I am not sure how I can calculate the sampling rate in this case?
Also, if I have a varying sampling rate, can I use still Butterworth?

Related

Does the duration of a signal affect its frequency component's amplitude? Also, does the sampling frequency affect the power of a signal?

I have two questions that are bugging me:
Does the duration of an audio signal affect the amplitude of frequency components of that same signal? For example, I am recording the sound of a fan using a microphone. At first, I record only for 10 sec and convert the audio signal into frequency spectrum. Then, I record the same sound for 20 sec and then convert the audio signal into frequency spectrum. In both the cases, the sound of the fan is same, but does the duration of the signal affect the amplitude of frequency components in the spectrum plot?
For example, I have 2 audio signals. For the first one, I have that same fan sound recording for 10 sec and the sampling frequency is 5KHz, and for the second recording, I have that same audio signal but now the sampling frequency is changed to 15KHz. I used MATLAB to check the power for both the signals and the power for both the signals was same, however I want to know why. Formula that I used was Power=rms(signal)^2. According to me the second signal should have more power because now there are more number of samples compared to the first recording and since those extra samples would also have a random amplitude, the average shouldn't be the same as for the first one. Am I thinking it right?
Can anyone provide their thoughts? Thank You!
This answer is from: https://dsp.stackexchange.com/questions/75025/does-the-duration-of-a-signal-affect-its-frequency-components-amplitude-also
Power is energy per unit time. If you increase the duration, you increase the energy, but due to the normalization with time the power would be the same.
The DFT as given by
X[k]=∑n=0N−1x[n]e−j2πnk/N
will scale the frequency component by N as given by the summation over N samples. This can be normalized by multiplying the result by 1/N.
The frequency components of the signal levels will be the same in a normalized DFT (normalized by dividing by the total number of samples) for signal components that occupy one bin (pure tones), but the noise floor as observed may be lower by the change in sampling rate: if the noise floor is limited by quantization noise, the total quantization noise (well approximated as white noise, meaning constant across all frequencies) will be spread over a wider frequency range, so increasing the sampling rate will cause the contribution of quantization noise on a per Hz basis (noise density) to be lower. Further the duration will effect the frequency resolution in each bin in the frequency domain; for an unwindowed DFT, the equivalent noise bandwidth per bin is fs/N where fs is the sampling rate and N is the number of bins. At a given sampling rate, increasing the number of bins will increase the total duration and thus reduce the noise bandwidth per bin; causing the overall DFT noise floor as observed to decrease. (Same noise power just less measured in each bin). Windowing if done in the time domain will reduce the signal level by the coherent gain of the window, but increase the equivalent noise bandwidth such that pure tones will be reduced more than noise that is spread over multiple bins.

How to generate accurate FFT plot of guitar harmonics with only 256 data points # 44.1khz Fs ?[Matlab]

I'm trying to make a realtime(ish) monophonic guitar to midi program. I want a latency of <=6 milli secs. To find what note was played i aim to sample 256 points (should take approx 6 millis) , run an fft and analyze mag plot to determine pitch of note played.
When i do this in matlab, it gives me back very unstable/inaccurate results with peaks appearing in random places etc.
The note being inputted is 110Hz sampled # 44.1khz. I've applied a high pass filter at 500hz with a roll off of 48db/octave.. so only the higher harmonics of signal should remain. The audio last for 1 second ( filled with zeros after 256 samples)
Code:
%fft work
guitar = wavread('C:\Users\Donnacha\Desktop\Astring110hz.wav');
guitar(1:44100);
X = fft(guitar);
Xmag = abs(X);
plot(Xmag);
Zoomed in FFT plot
I was hoping to see all the harmonics of 110Hz (A note on guitar) starting at >500hz..
How would i achieve accurate results from an FFT with such little data?
You can't. (at least reliably for all notes in a guitar's range).
256 samples at 44.1kHz is less than one period of most low string guitar notes. One period of vibration from a guitar's open low E string takes around 535 samples, depending on the guitar's tuning and intonation.
Harmonics often require multiple periods (repetitions) of a guitar note waveform within the FFT window in order to reliably show up in the FFT's spectrum. The more periods within the FFT window, the more reliably and sharper the harmonics show up in the FFT spectrum. Even more periods are required if the data is Von Hann (et.al.) windowed to avoid "leakage" windowing artifacts. So you have to pick the minimum number of periods needed based on the lowest note needed, your window type, and your statistical reliability and frequency resolution requirements.
An alternative is to concatenate several sets of your 256 samples into a longer window, at least as long as several periods of the lowest pitch you want to reliably plot.

Add noise to speech with different sampling rates in matlab

I am trying to add a noise file (.wav) file to a speech signal(.wav).
[b fs]=audioread('AzBio_01-01_S60.wav');
[babble fs1]=audioread('cafeteria_babble.wav');
The issue is that both the files have different sampling rates (fs=22050, fs1=44100).
When i add them it distorts the other signal since the sampling rate is different. How do i solve this. I am playing the sound via
p=audioplayer (total,fs)
play (p)
The distortion will be due to the sample values summing to more than 1 not the sample rate differences. To deal with that risk divide both vectors by two before summing them.
That said, you do still need to deal with the sample rate difference. There are two ways of doing this: either reduce the sampling rate of the signal with the higher sampling rate or increase the sampling rate of the other signal.
Reducing the sampling rate of the signal with the higher rate is easier but comes with the disadvantage that you are actually losing data. You could do this by using resample. resample(babble, fs, fs1)
Alternatively you can upsample the signal with the lower sampling frequency. This won't give you any more data but it will mean the sample rates match. You can do this with interp1. b = interp1(1:length(b),b,0.5:0.5:length(b))
So in summary
[b fs]=audioread('AzBio_01-01_S60.wav');
[babble fs1]=audioread('cafeteria_babble.wav');
b = interp1(1:length(b),b,0.5:0.5:length(b)); % interpolate b to fs1
total = b/2 + babble/2; % sum at half the sample values
p=audioplayer (total,fs1);
play (p)

Specific part of an impulse response matlab

I measure the impulse response of a microphone. At beginning of plot i have some delay and then highest value comes at 40 ms and decreases till 45 ms.
When I take fourier transform, I only want to use the part between 35-45 ms. when I use different microphones, delay decreases or increases so the peak value is shifting. So, the time range I want also changes. How I can get that range, which contains highest value, automaticaly in MATLAB?
Assuming you have a vector y of measurements and want an interval of length 2r+1 measurements you could do:
center = find(y==max(y)); % find the peak value
y_edited = y(center-r:center+r); % look at r samples before and after this peak
Then perform the fourier transform on y_edited. Note that having noise on your signal might affect your performance.

FFT faulty results for high frequencies?

I have sampled the 50Hz output current of an inverter with sampling frequency of 50 KHz for 1 minute. I am supposed to divide the time to 200ms packages(0.2s or 10 periods of the main signal)and do the FFT on each package. So it means that I have 10000 samples in each package (if not I zero pad or truncate ,that does not make a big difference). I am also supposed to extract the frequency spectrum up to 9 KHz. Results are ok for low frequencies but I have wrong results (the values are the half of what for high frequency). Could you help me to understand what am I doing wrong?!
I have an idea, maybe some thing like the code below is happening to my FFT. Just change SF to 10000 and look how the results will be changed !
in this code if you change the SF(sampling frequency) from 30000 to 10000,
the results for high frequncies will be distorted and disordered . why ?
SF = 30000; %sampling frequency
% signal
t = 0:1/SF:1-1/SF; % sample points
wave=15*sin(2*pi*1*t)+1*sin(2*pi*123*t)+2*sin(2*pi*203*t)+3*sin(2*pi*223*t)+4*sin(2*pi*331*t)+5*sin(2*pi*2812*t)+6*sin(2*pi*5752*t)+7*sin(2*pi*7993*t);
wavefft = fft(wave);
L=floor(size(wave,2)/2)+1; % removing the mirror side of spectrum
MagSpec = abs(wavefft(1:L))/(SF/2); %% removing the mirror side of the spectrum
and ranging the domain
plot(MagSpec);
What you are observing is aliasing.
As you can see by comparing the results using a sampling rate of 50kHz
and that of using a sampling rate of 10kHz
The sinusoidals signals whose frequency where below half the sampling rate of 10kHz (Nyquist frequency), that is the sinusoidals at 1Hz, 123Hz, 203Hz, 223Hz, 331Hz and 2812Hz are not affected. The ones at 5752Hz and 7993Hz are aliased to 4248Hz and 2007Hz respectively.
You can still perform the FFT on 200ms or 10000 samples, but the sampling rate remains the same at 50kHz. That is you would have:
SF = 50000; %sampling frequency
% signal
t = 0:1/SF:1-1/SF; % sample points
wave=15*sin(2*pi*1*t)+1*sin(2*pi*123*t)+2*sin(2*pi*203*t)+3*sin(2*pi*223*t)+4*sin(2*pi*331*t)+5*sin(2*pi*2812*t)+6*sin(2*pi*5752*t)+7*sin(2*pi*7993*t);
for the signal generation, but you would split the resulting wave signal in chunks for your processing:
for i=1:floor(length(wave)/10000)
wavefft = fft(wave(1+(i-1)*10000:i*10000))
% do somthing with the wavefft result
end