How to generate a square wave with specif values

4 ビュー (過去 30 日間)
Edoardo Demattei
Edoardo Demattei 2021 年 1 月 29 日
回答済み: Suraj Kumar 2025 年 3 月 27 日
How to generate a square wave witch start from c0 and reaches c0+Vmax? Thanks for the support
N=1e5
Np=3
Fx=Np/N
n=(0:N-1)';
wt=2*pi*Fx*n;
c0=2
Vmax=5
phi1=pi/3
x_square=

回答 (1 件)

Suraj Kumar
Suraj Kumar 2025 年 3 月 27 日
Hi Edoardo,
To generate a square wave in MATLAB that starts at a baseline value c0 and reaches a maximum value of c0 + Vmax, you can check the following steps:
1. Define the necessary parameters required for generating the square wave and create a time vector.
N = 1e5;
Np = 3;
Fx = Np / N;
n = (0:N-1)';
wt = 2 * pi * Fx * n;
c0 = 2;
Vmax = 5;
phi1 = pi/3;
2. Calculate the angular frequency component and generate the square wave using the square function. Transform the wave to oscillate between c0 and c0 + Vmax.
% Generate square wave
x_square = c0 + (Vmax/2) * (1 + square(wt + phi1));
3. Plot the square wave to visualize it.
% Plot the square wave
plot(n, x_square);
xlabel('Sample Index');
ylabel('Amplitude');
title('Square Wave');
grid on;
Hope this solves your query!

カテゴリ

Help Center および File ExchangeSignal Processing Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by