Rolegur
Rolegur

Reputation: 85

How can I create a sine wave plot for audio. If I have the frequency and length of the sound recording

For now, I have code that shows the magnitude and its frequency

def plot_fft(p, xf, fs, notes, dimensions=(960, 540)):
    xf_bet=xf-160
    layout = go.Layout(
        title="frequency spectrum",
        autosize=False,
        width=dimensions[0],
        height=dimensions[1],
        xaxis_title="Frequency (note)",
        yaxis_title="Magnitude",
        font={'size': 24}
    )

    fig = go.Figure(layout=layout,
                layout_xaxis_range=[FREQ_MIN, FREQ_MAX],
                layout_yaxis_range=[0, 1]
                )

    fig.add_trace(go.Scatter(
        x=xf_bet,
        y=p))

    for note in notes:
        # Dodanie wartości częstotliwości do nazwy dźwięku
        note_name_with_freq = f"{note[1]} ({round(note[0]-160, 2)} Hz)"
        fig.add_annotation(x=note[0] + 10, y=note[2],
                       text=note_name_with_freq,
                       font={'size': 48},
                       showarrow=False)
    return fig

The idea of the program is to create many plots and ultimately combine them into an MP4

Upvotes: 0

Views: 59

Answers (1)

StephanT
StephanT

Reputation: 833

I can think of two ways:

  1. Simply create the individual sine waves. Each frequency wave is then

    y = A * sin(2 * np.pi * f + phi)

with amplitude A, frequency f and phase-shift phi.

  1. If you have a Fourier spectrum (graph of all frequencies and amplitudes), apply the inverse Fourier Transform (iftt) to get the real signal.

Upvotes: 1

Related Questions