필터 지우기
필터 지우기

How to interpolate the given plot?

조회 수: 5 (최근 30일)
JD_PM
JD_PM 2022년 4월 28일
댓글: Star Strider 2022년 4월 28일
I plot speed vs. width (the first three columns are the velocity components and the last the width, please see first file here https://drive.google.com/drive/folders/1CgeDuCahVZvLXXkNkGWlPg8lSGyurmI3) using the following code
clc
close all
clear all
warning('OFF', 'MATLAB:table:ModifiedAndSavedVarnames')
%step 0
Qi = readmatrix("004timestepN2Void30mm.csv");
[v,w] = size(Qi);
%step 1
fCom_004 = Qi(:,1);
sCom_004 = Qi(:,2);
tCom_004 = Qi(:,3);
width_004 = [0;Qi(:,w)+0.0075];
U_004 = sqrt(fCom_004.^2 + sCom_004.^2 + tCom_004.^2);
refined_U_004 = [0;U_004];
%step 3
figure
plot(width_004,refined_U_004)
legend('0.004 timestep','Location','bestoutside')
xlabel('width [m] | 30mm profile')
ylabel('U magnitude [m/s]')
axis square;
grid on;
You'll see that the peak is far from being smooth. To fix the issue I am trying to use interpolation. To do so I modified step 3
newX = linspace (0, 0.015,3e3);
newY = interp1(width_0002,refined_U_0002,newX);
figure
plot(newX,newY)
legend('0.004 timestep','Location','bestoutside')
xlabel('width [m] | 30mm profile')
ylabel('U magnitude [m/s]')
axis square;
grid on;
But the following error pops up "Sample points must be unique."
I think I know why this is happening.
By default, Matlab seems to only read the first four decimals of a given number. Notice that in the last column many numbers only differ from the fifth number; that's why Matlab sees them as equal.
Assuming that I am right, how to make Matlab read the full given numbers of a file?
Thanks in advance! :)

채택된 답변

Star Strider
Star Strider 2022년 4월 28일
MATLAB reads what it is given.
%step 0
Qi = readmatrix("JD_PM sampleData30mm.csv");
[v,w] = size(Qi);
%step 1
fCom_004 = Qi(:,1);
sCom_004 = Qi(:,2);
tCom_004 = Qi(:,3);
width_004 = [0;Qi(:,w)+0.0075];
U_004 = sqrt(fCom_004.^2 + sCom_004.^2 + tCom_004.^2);
refined_U_004 = [0;U_004];
%step 3
figure
plot(width_004,refined_U_004)
legend('0.004 timestep','Location','bestoutside')
xlabel('width [m] | 30mm profile')
ylabel('U magnitude [m/s]')
axis square;
grid on;
% % You'll see that the peak is far from being smooth. To fix the issue I am trying to use interpolation. To do so I modified step 3
newX = linspace (0, 0.015,3e3);
[width_004,idx] = unique(width_004, 'stable'); % Eliminate Duplicates
newY = interp1(width_004,refined_U_004(idx),newX);
figure
plot(newX,newY)
legend('0.004 timestep','Location','bestoutside')
xlabel('width [m] | 30mm profile')
ylabel('U magnitude [m/s]')
axis square;
grid on;
I tried different interpolation methods ('pchip', 'spline') and do not see a difference.
.
  댓글 수: 2
JD_PM
JD_PM 2022년 4월 28일
Thank you Star Strider. As interpolation seems to not help to get a smoother peak, do you know of an alternative method to do so?
Star Strider
Star Strider 2022년 4월 28일
My pleasure!
One option is to develop a nonlinear model for that and fit the data to it.
Another option is to use the Signal Processing Toolbox sgolayfilt funciton —
%step 0
Qi = readmatrix('https://www.mathworks.com/matlabcentral/answers/uploaded_files/982200/JD_PM%20sampleData30mm.csv');
[v,w] = size(Qi);
%step 1
fCom_004 = Qi(:,1);
sCom_004 = Qi(:,2);
tCom_004 = Qi(:,3);
width_004 = [0;Qi(:,w)+0.0075];
U_004 = sqrt(fCom_004.^2 + sCom_004.^2 + tCom_004.^2);
refined_U_004 = [0;U_004];
%step 3
figure
plot(width_004,refined_U_004)
legend('0.004 timestep','Location','bestoutside')
xlabel('width [m] | 30mm profile')
ylabel('U magnitude [m/s]')
axis square;
grid on;
% % You'll see that the peak is far from being smooth. To fix the issue I am trying to use interpolation. To do so I modified step 3
newX = linspace (0, 0.015,3e3);
[width_004,idx] = unique(width_004, 'stable'); % Eliminate Duplicates
newY = sgolayfilt(refined_U_004(idx), 3, 81); % Use 'sgolayfilt'
figure
plot(width_004,newY)
legend('0.004 timestep','Location','bestoutside')
xlabel('width [m] | 30mm profile')
ylabel('U magnitude [m/s]')
axis square;
grid on;
Change the ‘framelen’ value to get the desired result.
The smoothdata function has an ‘sgolay’ option, however I do not know if that requires the Signal Processing Toolbox as well.
.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Visual Exploration에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by