Single cycle PWM wave generation

I am working on Ultrasonic testing . We need to send the PWM pulses(one/two/three cycles) to the transducer and then to waveguide and receive the echos back and after receiving the echos we again need to send the PWm pulses.I am able to generate a code which through which I am able to generate infinite PWM signal of our desired frequency and dutycylce.But the problem is I need to send few pulses(not more than 3) to transducer.Can you please help me with the code how to generate few pulse cycles of PWM.

Count the number of generated cycles and stop when the limit is reached. For more detailed help show your already existing code.

Cycles of what?

Processor cycles? PWM cycles? Other kind of cycles?

Frequency and duty cycle?

#include <PWM.h>
int led=9;
int i;
int32_t frequency=50000;
//float t=1/frequency;
int duty=127;
void setup()
bool success=SetPinFrequencySafe(led,frequency);

digitalWrite(13,HIGH);// put your setup code here, to run on



void loop() {
// put your main code here, to run repeatedly:


Thank you for your response.This is the code that I used to generate infinite pulses continuous pwm signal of any frequency and dutycycle.But I need only few pulses (not more than three pulses) for our testing.I dont want infinite PWM signal.Can you help e with that how to generate only 2 pulses?

Generate the pulses in software, that's easier to count.

Can you please explain me in more detail on how to limit the number of cycles? It will be very helpful to me if you share a relevant code snippet also.

Thank you in advance.

Something like this, only without delay():

int count = 0;
while (count < numCycles) {
 digitalWrite(out, LOW);
 digitalWrite(out, HIGH);