Equivalent matlab function for python librosa.resample
조회 수: 19 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
채택된 답변
Mathieu NOE
2021년 8월 26일
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
Mathieu NOE
2021년 8월 31일
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 ?
Mathieu NOE
2021년 9월 2일
hello Betty
if my answer has fullfilled your expectations, do you mind accepting it ?
thanks
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!