Clear Filters
Clear Filters

can someone please assist to make the square wave plot at 50Hz

8 views (last 30 days)
fs = 1000;
f = 4; %amount of square waves of graph
t = 0:1/fs:1-1/fs;
y = 3 * square(2 * pi * f * t); %generating the square wave
plot(t, y, 'LineWidth', 2)
xlabel('Time (s)')
ylabel('Amplitude')
title('Square Wave with 50 Hz Frequency and 4 Hz Period')
grid on
ylim([-5, 5]) % Set y-axis limits
the code is above . just struggling to make the square wave plot at 50Hz . I dont seem to understand where to modify the code to plot the square wave at 50Hz

Answers (2)

Manikanta Aditya
Manikanta Aditya on 10 Apr 2024
Edited: Manikanta Aditya on 10 Apr 2024
Hi,
To plot a square wave at 50 Hz, you need to adjust the frequency value f in your code. The frequency of the square wave is proportional to the argument of the square function, which is 2*pi*f*t in your code.
fs = 1000; % Sampling frequency
f = 50; % Frequency of the square wave (50 Hz)
t = 0:1/fs:1-1/fs; % Time vector
y = 3 * square(2 * pi * f * t); % generating the square wave
plot(t, y, 'LineWidth', 2)
xlabel('Time (s)')
ylabel('Amplitude')
title('Square Wave with 50 Hz Frequency')
grid on
ylim([-5, 5]) % Set y-axis limits
Thanks!

Hassaan
Hassaan on 10 Apr 2024
fs = 1000; % Sampling frequency
f = 50; % Frequency of the square wave
% Time vector from 0 to 1 second, with step size of 1/fs
t = 0:1/fs:1-1/fs;
% Generating the square wave
% square(2 * pi * f * t) generates a square wave with amplitude between -1 and 1
% Multiplying by 3 scales the wave to be between -3 and 3
y = 3 * square(2 * pi * f * t);
% Plotting the square wave
figure; % Opens a new figure window
plot(t, y, 'LineWidth', 2, 'Color', 'b'); % 'k' stands for black color
hold on; % Keeps the plot for further additions
% Adding a zero line for reference, making it easier to see the wave oscillation
plot(t, zeros(size(t)), '--', 'Color', [0.5 0.5 0.5]);
hold off; % No more additions to this plot
% Enhancing plot aesthetics
xlabel('Time (s)', 'FontSize', 12, 'FontWeight', 'bold');
ylabel('Amplitude', 'FontSize', 12, 'FontWeight', 'bold');
title('50 Hz Square Wave Plot', 'FontSize', 14, 'FontWeight', 'bold');
grid on; % Adding a grid for better readability
% Setting y-axis limits
ylim([-5, 5]);
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

Tags

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!