How can I plot an impulse (sech(x)) consisting of 2002 points, where last 1001 point are zeros?

So, I've got 2 sech(x) impulses, 1-st consisting of 1001 points, and 2-nd, consisting of 2002 points. For the 2-nd one I need to set last (right) 1001 points as zeros. How can I do that?
Thank you!

 Risposta accettata

hello
if you have a recent release (Since R2023b) you can use paddata : here we pad the four first non zero values (your first sech array) with here 2 zeroes
A = [1; 3; 5; 7];
B = paddata(A,6)
B = 6×1
1 3 5 7 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

6 Commenti

if you are running an older relase you can achieve the same result with either padarray , horzcat , vertcat :
you just have to pay attention if the array are row or column oriented
IMHO, horzcat , vertcat are the two simplest options for what you need to do
A = [1; 3; 5; 7]; % vertical array
B = padarray(A,2,0,'post')
B = 6×1
1 3 5 7 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
B = vertcat(A,zeros(2,1))
B = 6×1
1 3 5 7 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
A = [1 3 5 7]; % horizontal array
B = horzcat(A,zeros(1,2))
B = 1×6
1 3 5 7 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Thank you! But I didnt mention, that I'm using linspace. I think, they might be incompatible somehow..
A = 1:1001; / A = 1001; % I've tried both
B = paddata(A,1001);
x1 = linspace (-10, 10, A);
x2 = linspace (-10, 10, B);
imp = sech(x1);
S0 = sech(x2);
plot(x1, imp, "blue", x2, S0, "red");
And got an error:
Inputs must be scalars.
hello again
maybe I didn't undertsand correctly what you wanted
I interpreted that S0 is imp for the first 1001 samples and the following 1001 samples would be zeros
but the code you share is simply generating a sech pulse of two different length ?
if that is what you want you don't need any of the above mentionned functions , you can simply do this
and what you get is the same sech pulse but with different resolution (same duration)
A = 1001;
B = 2002;
x1 = linspace (-10, 10, A);
x2 = linspace (-10, 10, B);
imp = sech(x1);
S0 = sech(x2);
plot(x1, imp, "*blue", x2, S0, "red","linewidth",2);
and if you wanted to put the last 1001 sample of the second data to zero , this is it :
A = 1001;
B = 2002;
x1 = linspace (-10, 10, A);
x2 = linspace (-10, 10, B);
imp = sech(x1);
S0 = sech(x2);
% put the last N points to zero
N = 1001;
S0(end-N+1:end) = 0;
plot(x1, imp, "*blue", x2, S0, "red","linewidth",2);
Mathieu, thank you very much! It is really helpful!

Accedi per commentare.

Più risposte (2)

Mathieu has shown you how to do that in MATLAB. This is the standard approach in MATLAB computing. However, if you are looking for a math function to include in your thesis or journal paper, you might consider solving the following puzzle:
What input to the hyperbolic secant function will yield when and output zero otherwise?
ub = 10; % upper bound
x = linspace(-ub, ub, 2001); % domain
figure(1)
plot(x, sech(x)), grid on
xlabel('x'), ylabel('sech(x)')
title('Standard Hyperbolic Secant function')
ylim([-0.2, 1.2])
From the plot, since we know that converges to zero at , or for , we can design an intermediate function for the input to the hyperbolic secant such that produces the desired output values within the domain. One approach is to use the signum function to create a step at such that for .
f = x + ub/2*sign(x) + ub/2;
y = sech(f);
figure(2)
subplot(211)
plot(x, f), grid on
xlabel('x'), ylabel('f(x)')
title('Intermediate function, f(x)')
subplot(212)
plot(x, y), grid on
xlabel('x'), ylabel('y')
title('Desired Output')
ylim([-0.2, 1.2])

4 Commenti

Sam, thank you very much! Wow, that's mindblowing:)
Yes, the formula is going to be useful!
it is for sure a great idea (and I have to recognize it's not what I would have found all by myself !)
NB nevertheless that the y data for x>0 is not truly zero , so it depends if its just for plotting or if you will be using this data later in another computation.
just my 2 cents
ub = 10; % upper bound
x = linspace(-ub, ub, 2001); % domain
f = x + ub/2*sign(x) + ub/2;
y = sech(f);
xmin = 0;
plot(x(x>xmin),y(x>xmin))
Thank you, @Mathieu NOE! Yes, that's important notice. Not sure for further computations, but indeed it is worth mentioning!

Accedi per commentare.

@Mathieu NOE indeed has a point. My proposed approach does not give an absolute zero. If you require absolute zero for , then you should consider using this alternative approach, which also reflects my mathematical interpretation of the MATLAB indexing trick:
Note: You can imagine that acts like a lowpass filter but allows all signals pass for . For your info, "Lowpass" is an engineering term, typically used in E&E Engineering and Audio Engineering.
ub = 10; % upper bound
x = linspace(-ub, ub, 2001); % domain
f1 = 1 - heaviside(x); % function 1: Unit Step down
f2 = sech(x); % function 2: Hyperbolic Secant
y = f1.*f2;
subplot(211)
plot(x, f1), grid on
xlabel('x')
title('Unit Step down function (Heaviside)')
ylim([-0.2, 1.2])
subplot(212)
plot(x, y), grid on
xlabel('x')
title('Semi-Hyperbolic Secant function')
ylim([-0.2, 1.2])

1 Commento

Hi, @Sam Chak! Thank you very much for another mindblowing approach!:)
Another gratitude from my Sc.advisor!

Accedi per commentare.

Categorie

Scopri di più su MATLAB in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by