Equivalent matlab function for python librosa.resample

I need to upsample my original data of 250Hz to 96000Hz.I did it in python using librosa.resample. But when I did it in matlab I am not getting a smooth signal.I need to get an exact replica of upsampled output that I got in python.
Here i am attaching a data.mat file. In the data.mat, UL_filter is the input at sampling rate 250Hz and x is the upsampled signal which i got in python. I need to upsample the UL_filter signal @250Hz to 96000Hz and which should be same to my python output. In python, i have used the code librosa.resample(UL_filter,250,96000) and in matlab i have used resample.
To get an exact replica of python upsampled output , what function I have to use in matlab? Is there any equivalent function available in matlab for librosa.resample?

 Akzeptierte Antwort

Mathieu NOE
Mathieu NOE am 26 Aug. 2021
hello Betty
you can do this to upsample the data :
n = length(UL_filter);
nx = length(x);
% code for upsampling data
x_original = (0:n-1);
fs_original = 250;
fs_new = 96000;
fs_ratio = fs_new/fs_original;
x_new = linspace(0,n-1,n*fs_ratio);
upsampled_data = interp1(x_original,UL_filter,x_new,'linear');
plot(x_original,UL_filter,'*b',x_new,upsampled_data)
legend('original data','upsampled data');

5 Kommentare

Thank you for the answer. I tried this code, the ruggedness of the curve reduced. But I need an upsampled output which is exactly similar to the python output(x in the data.mat). In librosa.resample,by default, this uses a high-quality (but relatively slow) method (‘kaiser_best’) for band-limited sinc interpolation. How can we do it in the MATLAB?
hello
well, how close can we get to the python output, I don't know as I don't have the values python will generate
If you have those data , I can see where we are and if we need to refine the matlab method
are you able to provide a test case with the data python generates ?
hello,
Here I am attaching a figure which is the comparison plot of python and matlab upsampled outputs. I am getting a smooth curve (green) while upsampling in python whereas an oscillating output(red) in matlab. In data.mat, python upsampled output is given as 'x' and 'UL_filter' is the input. We need to upsample the UL_filter from 250Hz to 96000Hz and need to obtain an output similar to 'x'. I am trying to replicate the python output in matlab. Here I am attaching the documentation of librosa.resample that i have used in python to upsample.
hello
funny I don't see those "waves" in my linear interpolated data (red curve).
FYI, a linear interpolation will give "straight" lines between the 250 Hz sampled data - and this is what I get when I zoom in the same x axis interval as in your figure
Second, I modified a bit the code and selected also a spline interpolation method for a smoother ouput
code :
clc
clearvars
load('data.mat');
n = length(UL_filter);
nx = length(x);
% code for upsampling data
fs_original = 250;
dt_original = 1/fs_original;
fs_new = 96000;
dt_new = 1/fs_new;
time_axis_original = (0:n-1)*dt_original;
time_axis_new = (0:nx-1)*dt_new;
upsampled_data = interp1(time_axis_original,UL_filter,time_axis_new,'spline'); % select interpolation method among the list below
% 'linear' - (default) linear interpolation
% 'spline' - piecewise cubic spline interpolation (SPLINE)
% 'pchip' - shape-preserving piecewise cubic interpolation
% 'cubic' - cubic convolution interpolation for uniformly-spaced
% data. This method does not extrapolate and falls back to
% 'spline' interpolation for irregularly-spaced data.
% NOTE: 'cubic' changed in R2020b to perform cubic convolution.
% In previous releases, 'cubic' was the same as 'pchip'.
% 'v5cubic' - same as 'cubic'
% 'makima' - modified Akima cubic interpolation
plot(time_axis_original,UL_filter,'*b',time_axis_new,x,'*g',time_axis_new,upsampled_data,'r')
legend('original data','python data','matlab upsampled data');
plot :
now you can see that the 3 data sets do perfectly overlay - python and matlab outputs are now equivalent.
there are other options you can try as well as "cubic" or "makina". We can compute the error between python and matlab outputs , but I also noticed that python generates some side effects due to it's sinc window : did you see the oscillations at the beginning and the end of you resampled data ?
hello Betty
if my answer has fullfilled your expectations, do you mind accepting it ?
thanks

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by