How to remove negative values using tanh and have higher y-axis values?
7 views (last 30 days)
Show older comments
I am new to Matlab and want to make a response time signal for our project using Matlab code like the same one on the image but with a filtered and smoother one. I have trouble making the y-axis the same as the image. Can you help me with my problem? Thank you.
x = linspace (117, 117.8);
e1 = tanh(117.3*(x-117.35)) + randn(size(x))/8;
plot(x,e1)
%xlim ([117 117.8]); ylim ([1900 1950]);
xlabel("Time (s)")
ylabel("Resistance (Ω)")

0 Comments
Accepted Answer
Image Analyst
on 4 Jan 2023
Try movmedian
x = linspace (117, 117.8, 1000);
e1 = tanh(117.3*(x-117.35)) + randn(size(x))/8;
plot(x,e1)
%xlim ([117 117.8]); ylim ([1900 1950]);
xlabel("Time (s)")
ylabel("Resistance (Ω)")
ySmooth = movmedian(e1, 31);
hold on
plot(x, ySmooth, 'r-', 'LineWidth', 2)
grid on;
xlabel('x');
ylabel('e1')
legend('e1', 'e1 smoothed', 'Location', 'northwest')
The median filter reduces noise while keeping true steps intact ans sharp (steep).
Change the window size to have more or less smoothing.
4 Comments
Image Analyst
on 4 Jan 2023
You can use ylim to set the lower and upper limit for the y axis. For example to have it display only in a viewport from 0 to 2:
ylim([0, 2]);
More Answers (1)
Sulaymon Eshkabilov
on 4 Jan 2023
Here is the corrected solution.
x = linspace (117, 117.8);
e1 = tanh(117.3*(x-117.35)) + randn(size(x))/8;
plot(x,e1*1e3) % Note that it was magnified by 1000 and thus, unit become mOhm
xlabel("Time (s)")
ylabel("Resistance (mΩ)") % Because of *1000 scaling factor
E1 = round(e1); % To display nice looking integer numbers
yticks(min(E1)*1e3-200:200:max(E1)*1e3+400), grid on
See Also
Categories
Find more on Multirate Signal Processing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
