1

I am computing PSD of a signal, and I want the power from frequency range 0Hz to 20Hz. This is what i tried using linspace

df = pd.read_csv(path)
df = pd.DataFrame(df)
x = np.linspace(0, 20, 41)
dt = x[1] - x[0]
fs = 1 / dt
f,P = signal.welch(df, fs=5, nperseg=30, noverlap=5,axis=0)

Here, I get 6 frequency components from 0Hz to 2.5Hz, but I want to compute the power for frequency range till 20Hz

Can anyone help me here to do the same.

Rashma N
  • 49
  • 6
  • So, what is the sampling frequency of your signal? – czr Sep 06 '18 at 11:44
  • I kept it for 5, as i have taken the signal with samples 30 for the explanation here, But originally the sampling frequency considered is 1024, for nperseg = 2048, noverlap=1024. – Rashma N Sep 06 '18 at 11:48
  • How many samples are in your dataframe df? – payne Sep 06 '18 at 12:07
  • The example explained in the question has 30 samples @payne – Rashma N Sep 06 '18 at 12:09
  • A think you told me the number of samples instead of the sampling frequency, how many samples does your signal have per second? Since you have 30 samples, how much time does this 30 samples last? – czr Sep 06 '18 at 12:14
  • one sec have 5 samples, Ok in the actual sense, one second has 1024 samples, I want to process the 2sec signal at a time. where i end up getting 1025 power and Frequency values. – Rashma N Sep 06 '18 at 12:16
  • @czr any suggestions ? – Rashma N Sep 06 '18 at 12:25
  • I can't quite understand, the sampling frequency is a fixed property related to how the signal was acquired , how can it be 5 and be 1024? – czr Sep 06 '18 at 12:31
  • @czr : It is fixed 1024. what i meant about 5 is which I took the example signal having the sampling frequency of 5Hz. leaving that 5Hz aside, The signal has 1024 samples per sec. I am processing 2sec's of data each time and overlapping 1sec. – Rashma N Sep 06 '18 at 12:35
  • The PSD only goes from 0 to fs/2, you should specify the correct sample frequency in the fs parameter, 1024 in your case. – czr Sep 06 '18 at 12:41

1 Answers1

1

The PSD only goes from 0 to fs/2, you should specify the correct sample frequency in the fs parameter, 1024 in your case.

This example illustrates how to get the PSD for a sinusoidal signal:

import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import welch

Fs = 1024 # Hz
Ts = 1/Fs
time = np.arange(0, 2, Ts) # 2 seconds

freqs = [20, 50, 100] # frequencies in Hz
x = np.zeros(len(time))
for f in freqs:
    x += np.sin(2 * np.pi * f * time)

plt.plot(x)

f, P = welch(x, fs=Fs)

plt.figure()
plt.stem(f, P)
czr
  • 658
  • 3
  • 13