I'm quite new to electronics in general.
My current circuit setup consists of an incoming sine wave. I use a comparator chip to compare this sin wave to a threshold which results in a square wave train, where the width of each square wave is changed by raising/lowering the threshold. I then use this output to trigger open/close a transistor to allow a separate signal through, which is the signal I wish to use. This setup works great when the frequency of the sin wave is low, however my issue is when running such that the incoming sin wave is higher frequency (on the order of 1-5 kHz) the signal looks like the image below.
Would there be any way to clean this up to remove the decreasing trains after the pulse? My initial though was perhaps the comparator chip, or the transistor cannot work at such frequencies but the spec sheet says they should be fine, and if this was the case I would expect a smooth tail not a drop to a value and then a decreasing tail.
edit:
Information that commenters asked for :) Comparator chip: LM311P Transistor: BC337
simulate this circuit – Schematic created using CircuitLab