# How can I set max & min value but maintain the curve trend?

9 views (last 30 days)
Vivian Yu on 19 Feb 2022
Answered: Catalytic on 19 Feb 2022
Hello everyone,
I have a fitting curve as you can see in figure below.
I want to make maximum value is equal to 1 and minimum value is equal to 0.
However, I also want to maintain the curve trend.
How can I do?
clear all
clc
clf
close all
load('wavelength.mat');
filename = ('PDA36A_spectrum2_sam.xlsx');
data = xlsread(filename);
points = data(:,1);
signal = data(:,2);
mask1 = signal>8000;
maskedSignal = signal(mask1);
maskedWave = wavelength(mask1);
coefficients = polyfit(maskedWave, maskedSignal, 5);
% Get smoothed signal.
smoothedSignal = polyval(coefficients,maskedWave');
figure
plot(maskedWave,smoothedSignal, 'r-', 'LineWidth', 2);
title('Weighting shape');
xlabel('wavelength (nm)');
ylabel('Ratio');
set(gca,'fontsize',15,'linewidth',1);
grid on
When I use function "interp1", the result is so bad.
smoothedSignal = smoothedSignal'./max(smoothedSignal);
range = linspace(0,1,length(smoothedSignal))';
spectruma=interp1(smoothedSignal,maskedWave,range,'spline'); %interpolate the exp data interp1(x,y,xi,method)
figure
plot(spectruma);
Thank you!
##### 1 CommentShowHide None
Walter Roberson on 19 Feb 2022
rescale()?

Sign in to comment.

### Accepted Answer

Catalytic on 19 Feb 2022
yourSignal = rescale(yourSignal)
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### More Answers (1)

Voss on 19 Feb 2022
clear all
clc
clf
close all
load('wavelength.mat');
filename = ('PDA36A_spectrum2_sam.xlsx');
data = xlsread(filename);
points = data(:,1);
signal = data(:,2);
mask1 = signal>8000;
maskedSignal = signal(mask1);
maskedWave = wavelength(mask1);
coefficients = polyfit(maskedWave, maskedSignal, 5);
Warning: Polynomial is badly conditioned. Add points with distinct X values, reduce the degree of the polynomial, or try centering and scaling as described in HELP POLYFIT.
% Get smoothed signal.
smoothedSignal = polyval(coefficients,maskedWave');
figure
plot(maskedWave,smoothedSignal, 'r-', 'LineWidth', 2);
title('Weighting shape');
xlabel('wavelength (nm)');
ylabel('Ratio');
set(gca,'fontsize',15,'linewidth',1);
grid on
% smoothedSignal = smoothedSignal'./max(smoothedSignal);
smoothedSignal = (smoothedSignal'-min(smoothedSignal))./(max(smoothedSignal)-min(smoothedSignal));
% smoothedSignal = rescale(smoothedSignal); % an alternative
% range = linspace(0,1,length(smoothedSignal))';
% spectruma=interp1(smoothedSignal,maskedWave,range,'spline'); %interpolate the exp data interp1(x,y,xi,method)
figure
% plot(spectruma);
plot(maskedWave,smoothedSignal);
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!