This paper describes a new technique for determining the optimal period of a pulsar and consequently its light curve. The implemented technique makes use of the Principal Component Analysis (PCA) applied to the so-called waterfall diagram, which is a bidimensional representation of the pulsar acquired data. In this context we have developed the python package pywpf to easily retrieve the period with the presented method. We applied this technique to sets of data of the brightest pulsars in visible light that we obtained with the fast photon counter Iqueye. Our results are compared with those obtained by different and more classical analyses (e.g., epoch folding), showing that the periods so determined agree within the errors, and that the errors associated to the waterfall-PCA folding technique are slightly smaller than those obtained by the $\chi^2$ epoch folding technique. We also simulated extremely noisy situations, showing that by means of a new merit function associated to the waterfall-PCA folding it is possible to get more confidence on the determined period with respect to the $\chi^2$ epoch folding technique.